Read unknown xml nodes

I’ve got this xml file

<notes>

<note>
<title>Dag 1</title>
<details>
<detail>Test A</detail>
<detail>Test B</detail>
</details>
</note>

<note>
<title>Dag 2</title>
<details>
<detail>Test C</detail>
<detail>Test D</detail>
<detail>Test E</detail>
</details>
</note>

</notes>

Which I’m reading with this simplexml php script:

<?php

// load XML file
$xml = simplexml_load_file('test.xml') or die ("Unable to load XML!");

// access XML data


foreach($xml->note as $note){
    echo  "<h2>" . $note->title ."</h2>\
";

    foreach($note->details->detail as $detail){
        echo $detail."<br />\
";
    }
}


?>

Which works fine. But I was wondering: is it also possible to read an xml file without specifying the nodes? I’ll try to explain :slight_smile: I want it to open the xml file. But then I want it to read the nodes in the order they appear in the xml file. Without naming each node. So it has to figure out that upon opening the main ‘notes’ node, the first node is ‘note’, then upon every ‘note’ it has to figure out that it has to read ‘title’ and ‘details’ (and ‘detail’ nodes inside ‘details’). All without naming the nodes. In the end it has to list the node values in the order they appear in the xml file.

Do I make myself clear ? :wink: And is that possible?

Already found out:

<?php
 $reader = new XMLReader();
    $reader->open("test.xml");
    while ($reader->read()) {
      if ($reader->hasValue) {
          echo nl2br($reader->value);
        }
      }
?>

Sure, check out XMLReader and its comments. :slight_smile:

Man, I was just getting used to simpelxml and now there’s xmlreader haha

One thing though: I’ve now used this simple script which works fine and reads the xml from top to buttom.

<?php
   $xml = new XMLReader();
   $xml->open("test.xml");
   while ($xml->read()) {
   if ($xml->value!="") {
   echo $xml->value."\
";
}}
?>

The thing is though, in my xml file it also displays the nodes which do not have content. Like the ‘details’ node which doesn’t have a nodevalue yet, but instead has several ‘detail’ nodes which do have content.

I thought I would have fixed that with the if statement, but the source code still displays:

Dag 1




Test A


Test B


etc.
How can I make it skip the empty lines?