php - How to recursively walk tree with SimpleXMLIterator?


I have this sample code, why my code can find only test3 tag? Where are test and test2?

$iter = new RecursiveIteratorIterator(
    new SimpleXMLIterator(file_get_contents('./test.xml'))

foreach ($iter as $node) {
    echo "Tag found: ".$node->getName()."\n";


<?xml version="1.0" encoding="UTF-8"?>



When usingRecursiveIteratorIterator, the default mode is JUST to list the leaves( If you change the construction to

$iter = new RecursiveIteratorIterator(
        new SimpleXMLIterator($xml)
        , RecursiveIteratorIterator::SELF_FIRST);

This indicates to also list the various other parts of the structure and will output...

Tag found: test2
Tag found: test3

People are also looking for solutions to the problem: php - Facebook/Twitter OpenGraph not scraping images on Node.js/Angular.js web application


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.