Jump to content

SimpleXML question


dukesdemise

Recommended Posts

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/240541-simplexml-question/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/240541-simplexml-question/#findComment-1235565
Share on other sites

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;
         }
      }
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/240541-simplexml-question/#findComment-1235585
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/240541-simplexml-question/#findComment-1235588
Share on other sites

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.