robin18 Posted October 6, 2011 Share Posted October 6, 2011 Hello, I'm trying to read the data of a xml file with the help of simplexml. I'm having trouble reading subchilds. For example im trying to read the following xml: <root> <child> <subchild> <dataineed/> <dataineed/> <dtaineed/> <subchild> </child> </root> I'm using the following code: <?php $oLinkjes = new SimpleXMLElement('http://feedlink.php', 0, TRUE); $countcat = count($oLinkjes->subchild); echo $countcat; ?> It echo's the the result '0' which means that it does not successfully reads the subchild. Because of that i can't get the data i want. When i change subchild to child is does echo '1'. Anyone knows what I'm doing wrong here? I'm new to xml so there might be a easy fix to this. Quote Link to comment https://forums.phpfreaks.com/topic/248536-simplexml-problem/ Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 Your XML (if that is your actual document) is poorly structured, you are missing the closing tag on the <child> element. SimpleXMLElement will throw errors and give a bad result. Also, you are also incorrectly traversing the XML $countcat = count($oLinkjes->child->subchild); will return 1 as there is 1 <subchild> under <child> under <root>. Quote Link to comment https://forums.phpfreaks.com/topic/248536-simplexml-problem/#findComment-1276365 Share on other sites More sharing options...
robin18 Posted October 6, 2011 Author Share Posted October 6, 2011 Thanks a lot buddski! I totally understand now. The xml was just a quick example and not the actual xml where i work with. Thanks a lot for the quick response :-) Quote Link to comment https://forums.phpfreaks.com/topic/248536-simplexml-problem/#findComment-1276370 Share on other sites More sharing options...
robin18 Posted October 6, 2011 Author Share Posted October 6, 2011 Sorry for the double post but i got 1 last question. When u have the following .xml <people> <data> <name> <item1>data</item1> <item2>data</item2> <item3>data</item3> </name> <something> <some><data></some> </something> <name> <item1>dataineed</item1> <item2>dataineed</item2> </name> </data> </people> I am interested in getting the data from the second child called 'name'. I can get the data from the first child name, but i want the data from the second child called name. Quote Link to comment https://forums.phpfreaks.com/topic/248536-simplexml-problem/#findComment-1276453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.