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

发布于 2024-12-01 02:03:09 字数 2620 浏览 1 评论 0原文

我有一个具有这种结构的 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。

谢谢。

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>  \n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>  \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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素手挽清风 2024-12-08 02:03:09

有点长但对我有用

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

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

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;
}
星星的轨迹 2024-12-08 02:03:09

感谢您的所有建议。它们都为我提供了比以前更多的有关访问嵌套节点的信息。我接受了杰米建议的修改版本。对于每个社区节点,我都通过此循环访问 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>  \n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>  \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();

?>

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>  \n";
        echo "   <a href='#' onClick=\"add_url('".$node['ID']."');\">Add URL</a>  \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();

?>
凝望流年 2024-12-08 02:03:09

您可以尝试使用

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

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

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.

爱*していゐ 2024-12-08 02:03:09

我不太确定您到底在寻找什么,但这可能很有用:

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

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

HTH

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文