PHP:使用simplexml遍历XML文件的所有级别 [英] PHP: Using simplexml to loop through all levels of an XML file

查看:110
本文介绍了PHP:使用simplexml遍历XML文件的所有级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数使用simplexml返回XML文件中的第一级节点并将其写入无序列表:

I have a function which uses simplexml to return the first level of nodes in an XML file and write it into an unordered list:

function printAssetMap() {
    $xml = simplexml_load_file(X_ASSETS);
    $assets = $xml->asset;
    $html = '<ul>'."\n";
    foreach ($assets as $asset) {
        $html .= '<li id="asset'.$asset->asset_assetid.'"><ins>&nbsp;</ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a></li>'."\n";
    }//end foreach
    $html .= '</ul>'."\n";
    return $html;
}// printAssetMap()

我正在使用的XML具有嵌套节点:

XML I am using, that has nested nodes:

<?xml version="1.0"?>
<assets>
  <asset>
    <asset_name>Home</asset_name>
    <asset_url>/home</asset_url>
    <asset_assetid>1</asset_assetid>
  </asset>
  <asset>
    <asset_name>Projects</asset_name>
    <asset_url>/projects</asset_url>
    <asset_assetid>2</asset_assetid>
    <asset>
      <asset_name>Portfolio</asset_name>
      <asset_url>/projects/portfolio</asset_url>
      <asset_assetid>3</asset_assetid>
    </asset>
    <asset>
      <asset_name>Current Jobs</asset_name>
      <asset_url>/projects/current-jobs</asset_url>
      <asset_assetid>4</asset_assetid>
    </asset>
  </asset>
</assets>

现在,我开始添加当前返回的节点的子节点.有一种方法可以遍历xml文件中所有级别的子节点,即使我不知道有多少个级别,也可以将这些级别添加到我的列表中?

Now, I am starting to add child nodes of the nodes that I am currently returning. Is there a way to loop through ALL levels of child nodes in an xml file, even if I don't know how many levels there are, and add those to my list?

推荐答案

因此,基本上您需要做的是一个函数,该函数接受当前节点的每个<asset/>子节点,构建HTML,然后检查当前节点是否具有自己的孩子,并不断在树的深处递归.

So basically what you need to do is a function that takes each <asset/> child of current node, builds the HTML then checks if the current node has <asset/> children of its own and keeps recursing deeper down the tree.

这是您的操作方式:

function printAssetMap()
{
    return printAssets(simplexml_load_file(X_ASSETS));
}

function printAssets(SimpleXMLElement $parent)
{
    $html = "<ul>\n";
    foreach ($parent->asset as $asset)
    {
        $html .= printAsset($asset);
    }
    $html .= "</ul>\n";

    return $html;
}

function printAsset(SimpleXMLElement $asset)
{
    $html = '<li id="asset'.$asset->asset_assetid.'"><ins>&nbsp;</ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a>';

    if (isset($asset->asset))
    {
        // has <asset/> children
        $html .= printAssets($asset);
    }

    $html .= "</li>\n";

    return $html;
}

顺便说一句,我希望一个名为"printX"的函数实际上是 print 或回显某些内容,而不是返回它.也许您应该将这些函数命名为"buildX"?

By the way, I would expect a function named "printX" to actually print or echo something, rather than return it. Perhaps you should name those functions "buildX" ?

这篇关于PHP:使用simplexml遍历XML文件的所有级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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