Chris Middleton
Chris Middleton

Reputation: 5934

Getting duplicate results while traversing a PHP SimpleXML object

When I traverse the following tree: http://tuningcode.com/math/temp/library.xml, some of the results are doubled. That is, for instance, it prints out the "Set Theory" topic section twice, with all of its contents. You can see the problematic results here: http://tuningcode.com/math/temp/simple-parse.php.

Screenshot of the problem

results from the traversal are doubled

PHP

<?php

$library = simplexml_load_file('library.xml');
if($library == null){

    echo "uh oh, null library";

}

function traverseObject($object) 
{
    foreach ($object as $key => $value ){
        if($key == "title"){
            echo "<b>" . $object->title . "</b>";           
        } else if ($key == "topic"){
            // traverse child elements, if any
            echo "<ul>";
            foreach ( $object->topic as $num => $thatTopic ){
                echo "<li>";
                traverseObject($thatTopic);
                echo "</li>";
            }
            echo "</ul>";
        } else { // skip the rest for now
        }
    }
}

traverseObject($library);

?>

XML

<?xml version="1.0" encoding="UTF-8"?>
<library userid="095209376">
    <title>UserID095209376's Library</title>
    <topic>
        <title>Set Theory</title>
        <topic>
            <title>Axioms</title>
            <topic>
                <title>Axiom of Separation</title>
                <id>axiom-of-separation</id>
            </topic>
            <topic>
                <title>Axiom of Infinity</title>
                <id>axiom-of-infinity</id>
                <entry>
                    <title>Axiom of Infinity</title>
                    <section>
                        <title>History</title>
                        <body>
                            Long ago, in a galaxy far far away...
                        </body>
                    </section>
                </entry>
            </topic>
        </topic>
     </topic>
     <topic>
        <title>Analysis</title>
        <topic>
            <title>Normed Vector Spaces</title>
            <id>normed-vector-spaces</id>
            <topic>
                <title>Vector space</title>
                <id>vector-space</id>
                <entry>
                    <section>
                        <title>Definition</title>
                        <body>A vector space is a...</body>
                    </section>
                </entry>
            </topic>
        </topic>
     </topic>
</library>

Upvotes: 0

Views: 32

Answers (1)

Chris Middleton
Chris Middleton

Reputation: 5934

So my algorithm was wrong. Kind of hard to analyze what exactly wasn't working, but here is the correction:

Output

enter image description here

PHP

<?php

error_reporting(E_ALL);

$library = simplexml_load_file('library.xml');
if($library == null){

    echo "uh oh, null library";

}

function traverseObject($object) 
{
    echo "<ul>";
    foreach ($object as $key => $value ){
        if($key == "title"){
            echo "<li><b>" . $object->title . "</b></li>";          
        } else if ($key == "topic"){
            // traverse child elements, if any
            traverseObject($value);
        } else if($key == "id"){
            echo "<i>id: " . $value . "</i>"; 
        } else { // skip the rest for now
        }
    }
    echo "</ul>";
}

traverseObject($library);

?>

Upvotes: 2

Related Questions