遍历PHP SimpleXML对象时获取重复结果

时间:2014-04-20 23:25:19

标签: php xml parsing

当我遍历以下树时:http://tuningcode.com/math/temp/library.xml,一些结果加倍。也就是说,例如,它打印出" Set Theory"主题部分两次,包含其所有内容。您可以在此处查看有问题的结果:http://tuningcode.com/math/temp/simple-parse.php

问题的屏幕截图

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>

1 个答案:

答案 0 :(得分:2)

所以我的算法错了。有点难以分析究竟什么不起作用,但这是纠正:

输出

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);

?>