English 中文(简体)
用PHP解析XML导航网站地图
原标题:Parsing an XML navigation sitemap with PHP

我正在从一个XML文件中实现一个PHP站点地图解析器。我做得比较好。但是,我需要解析器更加动态。我需要实现一个递归函数will,它将为找到的每个child_node继续循环。一个节点可以在另一个子节点中包含多个子节点。到目前为止,我所做的是为每个子节点实现一个具有不同变量名的单独foreach循环,但这是不可接受的,因为它不太灵活。

这是我的xml文件:

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

您可以注意到,节点联系人在child_node中有一个child_node。这就是我需要递归函数的地方。

这是我当前的PHP代码:

    $source =  sitemap.xml ;


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


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

        if ($node->child_node != "") {
            echo "$node->link<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/>";
        }
    }

这个PHP有正确的输出,但我必须为其父级child_node中的每个child_node创建另一个单独的foreach循环。有人能为我指明正确的方向吗?如何更改我的PHP代码,以便遍历网站地图中找到的子节点中的每个子节点?

非常感谢!

最佳回答

未测试。。。但应该起作用:

function print_node($node, $level){
 echo str_repeat("-",$level);
 echo "$node->link
";
 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);
问题回答

您将需要一个递归函数,并在处理每个顶级节点时调用它。

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

我将把如何使缩进工作作为一个有趣的练习留给读者





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签