4

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

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 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 文件中的所有级别的子节点,即使我不知道有多少级别,并将它们添加到我的列表中?

4

3 回答 3

8

所以基本上你需要做的是一个函数,它获取<asset/>当前节点的每个子节点,构建 HTML,然后检查当前节点是否有<asset/>自己的子节点,并继续递归到树的更深处。

以下是您的操作方法:

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”的函数实际打印或回显某些内容,而不是返回它。也许您应该将这些函数命名为“buildX”?

于 2010-01-24T23:19:11.390 回答
1

您需要使用递归函数。这是一个从 XML 输出数组的示例。它来自 PHP 文档 - http://www.php.net/manual/en/ref.simplexml.php。您可以修改它以输出列表。

<?php 
function XMLToArray($xml) 
{ 
  if ($xml instanceof SimpleXMLElement) { 
    $children = $xml->children(); 
    $return = null; 
  } 

  foreach ($children as $element => $value) { 
    if ($value instanceof SimpleXMLElement) { 
      $values = (array)$value->children(); 

      if (count($values) > 0) { 
        $return[$element] = XMLToArray($value); 
      } else { 
        if (!isset($return[$element])) { 
          $return[$element] = (string)$value; 
        } else { 
          if (!is_array($return[$element])) { 
            $return[$element] = array($return[$element], (string)$value); 
          } else { 
            $return[$element][] = (string)$value; 
          } 
        } 
      } 
    } 
  } 

  if (is_array($return)) { 
    return $return; 
  } else { 
    return $false; 
  } 
} 
?>
于 2010-01-24T22:54:23.607 回答
0
<?php
function all_nodes($xml){
    $all=array();
    function add($node){
        array_push($all,$node);
        foreach($node as $child){
            add($child);
        }
    }
    add($xml->documentElement);
    return $all;
}
?>

这将返回一个包含文档中所有节点的数组。

于 2012-02-16T21:06:58.143 回答