使用 PHP 解析 XML 导航站点地图 [英] Parsing an XML navigation sitemap with PHP

查看:38
本文介绍了使用 PHP 解析 XML 导航站点地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的 xml 文件:

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>

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

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

这是我当前的 PHP 代码:

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->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 代码以遍历站点地图中找到的 child_node 中的每个 child_node 的正确方向吗?

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?

非常感谢!

推荐答案

未测试...但应该可以:

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

这篇关于使用 PHP 解析 XML 导航站点地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆