使用 PHP 解析 XML 导航站点地图
我正在从 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 " " . $child->link . "<br/>";
foreach ($child->child_node as $innerchild) {
echo " " . $innerchild->link . "<br/>";
}
} else {
echo " " . $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未经测试...但应该有效:
Not tested...but should work:
您将需要一个递归函数,并在处理每个顶级节点时调用它。
我将把如何让缩进发挥作用作为一个有趣的练习留给读者。 ;-)
You'll want a recursive function, and call that as you process each top level node.
I'll leave as an interesting exercise to the reader how to get the indenting working. ;-)