沃梦达 / 编程问答 / php问题 / 正文

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

Accessing a nested node in an XML load by SimpleXMLElement(通过SimpleXMLElement访问XML加载中的嵌套节点)

本文介绍了通过SimpleXMLElement访问XML加载中的嵌套节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有此结构的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();

?>

这篇关于通过SimpleXMLElement访问XML加载中的嵌套节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过SimpleXMLElement访问XML加载中的嵌套节点

基础教程推荐