通过SimpleXMLElement访问XML加载中的嵌套节点

时间:2011-08-23 19:55:56

标签: php xml simplexml

我有一个具有这种结构的XML文件。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <COMMUNITIES>
    <COMMUNITY ID="c001">
      <NAME>Town Services</NAME> 
      <TOP>50</TOP> 
      <LEFT>50</LEFT> 
      <WIDTH>200</WIDTH> 
      <HEIGHT>300</HEIGHT> 
      <URLS>
          <URL ID="U001">
              <NAME>Google.com</NAME>
              <URL>http://www.google.com</URL>
          </URL>
          <URL ID="U002">
              <NAME>Bing.com</NAME>
              <URL>http://www.bing.com</URL>
          </URL>
          <URL ID="U003">
              <NAME>Yahoo.com</NAME>
              <URL>http://www.yahoo.com</URL>
          </URL>
          <URL ID="U004">
              <NAME>Aol.com</NAME>
              <URL>http://www.aol.com</URL>
          </URL>
      </URLS> 
      </COMMUNITY>
</COMMUNITIES>

我可以使用此脚本访问第一级元素NAME到HEIGHT。

<?php

function get_nodes() {
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);

foreach($nodes as $node) // loop through 
{

        echo "<div id = '".$node['ID']."' class= 'comdiv ui-widget-content' style = 'top: ".$node->TOP."px; left: ".$node->LEFT."px; width: ".$node->WIDTH."px; height: ".$node->HEIGHT."px;'> \n";

        echo "   <p class = 'comhdr editableText ui-widget-header'>".$node->NAME."</p>\n";

        echo "   <a href='#' onClick=\"delete_div('".$node['ID']."');\">Delete</a>&nbsp;&nbsp;\n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>&nbsp;&nbsp;\n";

        echo "</div> \n";

        echo "<script type='text/javascript'>\n";
        echo "  $('#".$node['ID']."').resizable();\n";
        echo "  $('#".$node['ID']."').draggable();\n";
        echo "  $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');\n";
        echo "</script>\n";


        $nodeid = $node['ID'];

        $xurls = $node->URL; // WHAT IS THE WAY TO GET TO <URLS> and LOOP THOUGH ALL <URL> NODES
        $nurls = sizeof($xurls);

}
        echo "<script type='text/javascript'>\n";
        echo "  $('.editableText').editableText();\n";
        echo "</script>\n";

   return;
}

echo get_nodes();
?>

如何获取节点中包含的节点数组?获得该数组后,我必须为每个URL检索NAME和URLC。

感谢。

4 个答案:

答案 0 :(得分:2)

稍微长一点,但对我有用

$nodes = new SimpleXMLElement('communities.xml', null, true);

foreach($nodes->COMMUNITY->URLS->URL as $url) {
 echo $url['ID'];
 echo $url->NAME;
 echo $url->URL;
}

答案 1 :(得分:1)

感谢所有建议。他们都给了我比以前更多关于访问嵌套节点的信息。我选择了Jamie建议的修改版本。对于每个社区节点,我使用此循环访问了URL。

    foreach($node->URLS->URL as $url)
    { 
       // Print HTML elements with the URL information
    }

这是完整的工作脚本。

<?php

function get_nodes() {
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);

foreach($nodes as $node) // loop through 
{

        echo "<div id = '".$node['ID']."' class= 'comdiv ui-widget-content' style = 'top: ".$node->TOP."px; left: ".$node->LEFT."px; 

width: ".$node->WIDTH."px; height: ".$node->HEIGHT."px;'> \n";

        echo "   <p class = 'comhdr editableText ui-widget-header'>".$node->NAME."</p>\n";

        echo "   <a href='#' onClick=\"delete_div('".$node['ID']."');\">Delete</a>&nbsp;&nbsp;\n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>&nbsp;&nbsp;\n";

        echo "</div> \n";

        echo "<script type='text/javascript'>\n";
        echo "  $('#".$node['ID']."').resizable();\n";
        echo "  $('#".$node['ID']."').draggable();\n";
        echo "  $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');\n";
        echo "</script>\n";

        foreach($node->URLS->URL as $url)
        { 
           echo "<script type='text/javascript'> alert('Node: ".$node['ID']." has URLS:".$url['ID']." ".$url->NAME." ".$url->URL." '); 

</script>";
        }


}
        echo "<script type='text/javascript'>\n";
        echo "  $('.editableText').editableText();\n";
        echo "</script>\n";

   return;
}

echo get_nodes();

?>

答案 2 :(得分:0)

我不太确定你在寻找什么,但这可能有用:

$node->children();返回节点的子节点,您可以轻松遍历:

foreach ($node->URLS->children() as $child)
{
    print $child->URL;
}

HTH

答案 3 :(得分:0)

您可以尝试使用

$urls = $node->xpath('URLS/URL');
foreach($urls as $url) {}

但似乎你的代码中有错误。而不是$ node-&gt; URL * S *;你使用$ node-&gt; URL;。尝试更换。