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

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>
