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

2022-04-13 00:00:00 xml php 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>

我可以通过此脚本通过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;'> 
";

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

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

        echo "</div> 
";

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


        $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'>
";
        echo "  $('.editableText').editableText();
";
        echo "</script>
";

   return;
}

echo get_nodes();
?>
如何获取包含在节点中的节点的数组?获得该数组后,我必须检索每个URL的名称和URLC。

谢谢。


解决方案

感谢您的所有建议。它们都为我提供了比以前更多的有关访问嵌套节点的信息。我接受了杰米建议的修改版本。对于每个社区节点,我使用此循环访问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;'> 
";

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

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

        echo "</div> 
";

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

        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'>
";
        echo "  $('.editableText').editableText();
";
        echo "</script>
";

   return;
}

echo get_nodes();

?>

相关文章