Good day. I understand simplexml, and faced the following problem. Suppose there is an xml file of the following content (a_xml.xml):
<?xml version="1.0" encoding="utf-8"?> <stat> <user> <cart>13</cart> <subid>2</subid> </user> <user> <cart>54</cart> <subid>-</subid> </user> </stat>
And there is the same simple php code:
<? $xml = simplexml_load_file('a_xml.xml'); foreach($xml->stat->user as $user) { echo $user->cart; } ?>
And at this moment I expect to see the numbers 13 and 54 on the screen. But in fact it turns out: " Warning: Invalid argument supplied for foreach () ". Why it happens? Where is the mistake?
And the strangest thing for me is that if you make a small change in the xml file, namely put the <stat>
tag inside the <rss>
, that is:
<?xml version="1.0" encoding="utf-8"?> <rss> <stat> <user> <cart>13</cart> <subid>2</subid> </user> <user> <cart>54</cart> <subid>-</subid> </user> </stat> </rss>
So everything works fine, the desired result appears on the screen. But naturally, I am not satisfied with such a way out of the problem. What to do?