dukesdemise Posted June 27, 2011 Share Posted June 27, 2011 I have some code that reads an XML file and prints out its contents. The problem is that not only do the contents get printed, but I am also getting empty fields printed too. I'm not sure how better to describe it than to publish the code below, and its output when run from a command line. Here is the code: <?php $first = new SimpleXMLElement ('simplexml_test.xml', NULL, true); echo " first desc = [" . $first->desc . "]" . PHP_EOL; foreach ($first->children() as $second) { echo "second desc = [" . $second->desc . "]" . PHP_EOL; foreach ($second->children() as $third) { echo " third desc = [" . $third->desc . "]" . PHP_EOL; echo " third data = [" . $third->data . "]" . PHP_EOL; } } ?> Here is the xml file that gets read in: <?xml version="1.0"?> <first> <desc>this is first</desc> <second> <desc>this is second</desc> <third> <desc>this is third</desc> <data>hello world</data> </third> </second> </first> And here is the output when run from a command line: venus$ php simplexml_test.php first desc = [this is first] second desc = [] second desc = [this is second] third desc = [] third data = [] third desc = [this is third] third data = [hello world] venus$ I would like to get rid of the secondary (and empty) fields. Any help would be appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 27, 2011 Share Posted June 27, 2011 These are all parent/child groupings. When you ask for the children of first, there are two: this is the first ... Then you try and echo out $second->desc. Your first element is the desc itself, and of course when you reference what in essence is $first->desc->desc, you get an empty result, because there is no child element of that name. Hope this clarifies it for you. Quote Link to comment Share on other sites More sharing options...
dukesdemise Posted June 27, 2011 Author Share Posted June 27, 2011 Thanks for the explanation. Would it be better to use XPath when trying to load nested XML data such as this into some sort of data structure? Quote Link to comment Share on other sites More sharing options...
dukesdemise Posted June 27, 2011 Author Share Posted June 27, 2011 BTW, I found this works well to keep the empties out. Again, thanks for your explanation. <?php $first = new SimpleXMLElement ('simplexml_test.xml', NULL, true); echo " first desc = [" . $first->desc . "]" . PHP_EOL; foreach ($first->children() as $second) { if ($second->count() > 0) { echo "second desc = [" . $second->desc . "]" . PHP_EOL; } foreach ($second->children() as $third) { if ($third->count() > 0) { echo " third desc = [" . $third->desc . "]" . PHP_EOL; echo " third data = [" . $third->data . "]" . PHP_EOL; } } } ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 27, 2011 Share Posted June 27, 2011 Thanks for the explanation. Would it be better to use XPath when trying to load nested XML data such as this into some sort of data structure? Totally up to you. If the structure is fixed, you can also just specify the names ala: $first->second->desc. Often what is being done with desc would be : Glad you got a solution. isset might be helpful as well, although I didn't test it. Quote Link to comment 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.