Jump to content

for loop in an XML doc


e1seix

Recommended Posts

OK guys. bit of a question here. i have an xml doc from which i lift info and update my database on a daily basis. i pretty much have the coding sorted to lift the info from within the various brackets, as such:

 

<prod awId="30876241" forSale="no" inStock="no" lang="EN" preOrder="no" webOffer="no">
  <pId>00001b</pId> 
  <ean>0000000000001</ean> 
  <name>AMERICAN CREW DAILY CONDITIONER (250ml)</name> 
  <desc>This daily conditioner instantly detangles your hair and creates a silky smooth feel.</desc> 
  <cat>
    <awCatId>118</awCatId> 
    <awCat>Haircare Products</awCat> 
    <mCat>Haircare</mCat> 
  </cat>
</prod>

 

now i can use this code to define the info within the brackets

 

$xml = simplexml_load_file('...filepath-for-xml-doc...');

foreach ($xml->xpath('//prod') as $character) {

	$awCat = $character->cat->awCat;
	$pId = $character->pId;
	$name = $character->name; }

 

Returning:

 

$awCat = Haircare Products

$pId = 00001b

$name = AMERICAN CREW DAILY CONDITIONER (250ml)

 

HOWEVER, if say I wanted to define an attribute of one of the brackets, not the content, say in the "prod" bracket the inStock="no" - how would i tweek the code?

 

all help appreciated.

 

cheers!

Link to comment
https://forums.phpfreaks.com/topic/137959-for-loop-in-an-xml-doc/
Share on other sites

oh many thanks! i have tried now to search for

 

foreach ($xml->xpath('//prod[@inStock="yes"]') as $character) {

 

as after all that saves me having to define the row and then define whether or not to mark it as in stock or out of stock. one prob though that keeps coming up... in this example:

 

<prod awId="32369745" forSale="no" inStock="no" lang="EN" preOrder="no" webOffer="no">

 

it only returns some of the attributes eg.

 

if i input:

 

('//prod[@inStock=no]') - it returns results, if however i enter:

('//prod[@webOffer=no]') - it returns nothing.

 

is there a rule for changing the above for attributes further along the prod bracket, or is there something wrong there? surely the above statement should technically return at least one result from the xml doc as that particular prod bracket is as it is, which puzzles me as to why it's being selective in displaying results...

ok, let's try and explain this in full. XML:

 

<prod awId="24484204" lang="EN" forSale="yes" preOrder="no" webOffer="no" inStock="yes">
  <pId>1048</pId> 
  <name>Cacharel Anais Anais Eau de Toilette Spray 30ml</name> 
</prod>

 

the two examples that return different results:

 

foreach ($xml->xpath('//prod[@awId="24484204"]') as $character) {

	$name = $character->name;

echo $name.'<br />';
}

 

returns the result...

 

 

foreach ($xml->xpath('//prod[@lang="EN"]') as $character) {

	$name = $character->name;

echo $name.'<br />';
}

 

returns nothing! despite both being more than valid... am i missing something very obvious? i genuinely have looked online for loopholes or something i'm doing wrong, but to no avail. what is it guys?!

 

??? yours confusled,

 

that code works for me:

<?php
$xml = <<<XML
<prods>
  <prod awId="24484204" lang="EN" forSale="yes" preOrder="no" webOffer="no" inStock="yes">
    <pId>1048</pId>
    <name>Cacharel Anais Anais Eau de Toilette Spray 30ml</name>
  </prod>
</prods>
XML;

$xml = simplexml_load_string($xml);

foreach($xml->xpath('//prod[@awId="24484204"]') as $character) {
  $name = (string)$character->name;
  echo $name.'<br />';
}
print '<hr>';
foreach ($xml->xpath('//prod[@lang="EN"]') as $character) {
  $name = (string)$character->name;
  echo $name.'<br />';
}
?>

 

what do the first few lines of the XML file look like?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.