Accessing a nested node in an XML load by SimpleXMLElement

橙三吉。 提交于 2021-01-29 06:50:52

问题


I have an XML file with this structure.

    <?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>

I am able to get to the first-level elements NAME through HEIGHT with this script.

<?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();
?>

How do I get an array of the nodes contained in the node? Once I get that array, I must retrieve the NAME and URLC for each URL.

Thanks.


回答1:


A little bit longer but works for me

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

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



回答2:


Thanks for all the suggestions. They all gave me more information than I had before about accessing nested nodes. I went with a modified version of Jamie's suggestion. With each community node I accessed the URLS with this loop.

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

Here is the working script in its entirety.

<?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();

?>



回答3:


You can try to use

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

But it seems that you have error in your code. Instead of $node->URL*S*; you use $node->URL;. Try to replace.




回答4:


I am not quite sure what you are exactly looking for but that could be useful:

$node->children(); returns the children of the node and you can easily loop through:

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

HTH



来源:https://stackoverflow.com/questions/7166769/accessing-a-nested-node-in-an-xml-load-by-simplexmlelement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!