Forum Moderators: coopster

Message Too Old, No Replies

SimpleXML loop

         

webfoo

3:38 pm on Apr 3, 2011 (gmt 0)

10+ Year Member



Is anything the matter with this? For some reason, nothing inside or after this loop will run in the code:

$xml=simplexml_load_string($somexml); 

$i=0;
foreach($xml->children()->children() as $child) {
if($i==0) { $someVar=$child; }
elseif($i==1) { $anotherVar=$child; }
elseif($i==2) { $varThree=$child->children(); }
elseif($i==3) { $myVar=$child; }
elseif($i==4) { $lastVar=$child; }
$i++;
}


It's supposed to grab each element (there are five) and assign it's value to a different variable.

Matthew1980

5:57 pm on Apr 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there webfoo,

Something about your logic looks iffy to me :/

Foreach's operate differently to a for loop; which I guess you are well aware of, but to me this looks like your using it in a kinda extraneous way, unless the counter is controlling something that "I can't see the wood for the tree's kinda way.."

Firstly, strip the code down to it's barest parts and run it through print_r() to see if there are values there:-

$xml=simplexml_load_string($somexml);

foreach($xml->children()->children() as $child) {
echo "<pre>";
print_r($child);
echo "</pre>";
}


See what that gives you, then if you have the data you expect, you can start to conditionally iterate through the data.

I hope I understood your question right :)

Though on second read, this line looks dubious:-

"elseif($i==2) { $varThree=$child->children(); }" shouldn't ->childern(); be removed?

Cheers,
MRb

webfoo

7:33 pm on Apr 3, 2011 (gmt 0)

10+ Year Member



When I echo $xml; it prints the entire XML string as it should. The root has five children, and each child has five children of its own. We are after the first set of grandchildren, or child[0]->children();

Matthew, your suggestion fails to output anything. I beleive it is a problem in the foreach argument. What should the argument be to loop through the first set of grandchildren?

As for varThree, this element actually contains a child of its own, which contains the data I need.

The XML we are looking at might contain different data every time the script is run, but the structure is always as follows:

<root>
<child>
<grandchild>someVar</grandchild>
<gradnchild>anotherVar</grandchild>
<grandchild><ggc>varThree</ggc></grandchild>
<grandchild>myVar</grandchild>
<grandchild>lastVar</grandchild>
</child>

[four more <child>s with identical structure....]

</root>

Matthew1980

8:40 pm on Apr 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there webfoo,

What happens if you print_r($xml); ? Hopefully you will/should get an associative array with your expected vars in place.

I guess that this is an example structure too, as there is a spelling error on grandchild ;)

I take it that there is some sort of numbering structure in place so that you can track through this xml doc? I'm quite intrigued by this now, I would have thought that doing a foreach would be the best way to approach this..

Possibly the fact that your using simplexml_load_string() and passing that into a foreach, when a foreach expects an array so that it can iterate through it? Try load file, especially if your using an xml file for this.

Sorry I can't be of more help; I shall research this is little as my xml usage is rusty...

Cheers,
MRb

webfoo

7:04 pm on Apr 4, 2011 (gmt 0)

10+ Year Member



Not sure what I did, but it's working now. That is the kind of thing that bugs me, because I can't know how to fix it next time. And yes, my mistake on the "gradnchild" tag.