通过 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>
我可以使用此脚本获取第一级元素 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> \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();
?>
如何获取节点中包含的节点数组?获得该数组后,我必须检索每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有点长但对我有用
A little bit longer but works for me
感谢您的所有建议。它们都为我提供了比以前更多的有关访问嵌套节点的信息。我接受了杰米建议的修改版本。对于每个社区节点,我都通过此循环访问 URL。
这是完整的工作脚本。
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.
Here is the working script in its entirety.
您可以尝试使用
但您的代码似乎有错误。而不是 $node->URL*S*;你使用$node->URL;。尝试更换。
You can try to use
But it seems that you have error in your code. Instead of $node->URL*S*; you use $node->URL;. Try to replace.
我不太确定您到底在寻找什么,但这可能很有用:
$node->children();
返回节点的子节点,您可以轻松循环: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:HTH