开发者

Parsing an XML navigation sitemap with PHP

I am implementing a PHP sitemap parser from an XML file. I am doing relatively well. However, I need the parser to be more dynamic. I need to implement a recursive function will which continue looping for every child_node which is found. A node can contain many child_nodes within another child_node. What I did till now was to implement a seperate foreach loop with different variable names for every child_node however this is not acceptable as it is not so flexible.

This is my xml file:

<sitemap>
    <node>
        <id>rootnode</id>
        <link>home.html</link>
    </node>
    <node>
        <id>about</id>
        <link>about.html</link>
    </node>
    <node>
        <id>contact</id>
        <link>contact.html</link>
        <child_node>
            <id>contact_uk</id>
            <link>contact_uk.html</link>
            <child_node>
                <id>customer_support_uk</id>
                <link>customer_support_uk.html</link>
            </child_node>
        </child_node>
        <child_node>
            <id>contact_usa</id>
            <link>contact_usa.html</link>
        </child_node>
    </node>

    <node>
        <id>products</id>
        <link>products.html</link>
    </node>
</sitemap>

You can note that the node contact has a child_node within a child_node. This is where I need to recursive function.

This is my current PHP code:

    $source = 'sitemap.xml';


    // load as file
    $sitemap = simplexml_load_file($source, null, true);


    foreach ($sitemap->node as $node) {

        if ($node->child_node != "") {
            echo "$node->开发者_如何学JAVAlink<br/>";
            foreach ($node->child_node as $child) {
                if ($child->child_node != "") {
                    echo "&nbsp;&nbsp;" . $child->link . "<br/>";
                    foreach ($child->child_node as $innerchild) {
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $innerchild->link . "<br/>";
                    }
                } else {
                    echo "&nbsp;&nbsp;" . $child->link . "<br/>";
                }
            }
        } else {
            echo "$node->link<br/>";
        }
    }

This PHP has the correct output but I have to create another seperate foreach loop for every child_node within its parent child_node. Can someone point me in the right direction on how to change my PHP code in order to traverse every child_node within a child_node found in the sitemap?

Many thanks!


Not tested...but should work:

function print_node($node, $level){
 echo str_repeat("-",$level);
 echo "$node->link\n";
 if ($node->child_node != "") {
       foreach ($node->child_node as $child) {
          print_node($child,$level+1);
       }
  }

}
$source = 'sitemap.xml';


$sitemap = simplexml_load_file($source, null, true);
foreach ($sitemap->node as $node)
    print_node($node,0);


You'll want a recursive function, and call that as you process each top level node.

function processChildren( $node )
{
    foreach( $node->child_node as $child )
    {
        echo "$child->link<br/>";
        if( count( $child->child_node ) )
        {
            processChildren( $child );
        }
    }
}

I'll leave as an interesting exercise to the reader how to get the indenting working. ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜