使用 PHP 解析 XML 导航站点地图

发布于 2024-10-18 02:34:33 字数 2363 浏览 3 评论 0原文

我正在从 XML 文件实现 PHP 站点地图解析器。我做得相对不错。但是,我需要解析器更加动态。我需要实现一个递归函数,它将继续循环找到的每个 child_node 。一个节点可以在另一个 child_node 中包含许多 child_node。到目前为止我所做的是为每个 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 代码以便遍历站点地图中找到的 child_node 内的每个 child_node 的正确方向吗?

非常感谢!

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->link<br/>";
            foreach ($node->child_node as $child) {
                if ($child->child_node != "") {
                    echo "  " . $child->link . "<br/>";
                    foreach ($child->child_node as $innerchild) {
                        echo "    " . $innerchild->link . "<br/>";
                    }
                } else {
                    echo "  " . $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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-10-25 02:34:33

未经测试...但应该有效:

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

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);
别把无礼当个性 2024-10-25 02:34:33

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

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

我将把如何让缩进发挥作用作为一个有趣的练习留给读者。 ;-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文