XML 在 PHP 中获取嵌套节点
在这个读取 XML 文件的 php 脚本中,用于访问嵌套节点的内部循环不返回任何数据。
PHP 脚本
<?php
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);
echo <<<EOF
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Top</th>
<th>Left</th>
<th>Width</th>
<th>Height</th>
<th>Urls</th>
</tr>
EOF;
foreach($nodes as $node) // loop through
{
echo "<tr>";
echo " <td>".$node['ID']."</td>";
echo " <td>".$node->NAME."</td>";
echo " <td>".$node->TOP."</td>";
echo " <td>".$node->LEFT."</td>";
echo " <td>".$node->WIDTH."</td>";
echo " <td>".$node->HEIGHT."</td>";
echo " <td>";
$urls = $node->URLS;
/* $urls = $xml->xpath('/COMMUNITIES/COMMUNITY/URLS/URL'); */
foreach($urls as $url)
{
echo ' '.$url->NAME;
}
echo ' </td>';
echo '</tr>';
}
echo '</table>';
?>
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>
<COMMUNITY ID="c002">
<NAME>Local Stores</NAME>
<TOP>50</TOP>
<LEFT>260</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Walgreens</NAME>
<URL>http://www.walgreens.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>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c003">
<NAME>Attractions</NAME>
<TOP>50</TOP>
<LEFT>470</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Museum</NAME>
<URL>http://www.mfa.org</URL>
</URL>
<URL ID="U002">
<NAME>Park</NAME>
<URL>http://www.bing.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c004">
<NAME>Online Stores</NAME>
<TOP>370</TOP>
<LEFT>50</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Amazon.com</NAME>
<URL>http://www.amazon.com</URL>
</URL>
<URL ID="U002">
<NAME>Target.com</NAME>
<URL>http://www.target.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c005">
<NAME>Online Forums</NAME>
<TOP>370</TOP>
<LEFT>300</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>200</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Technet</NAME>
<URL>http://www.Microsoft.com</URL>
</URL>
<URL ID="U002">
<NAME>MSDN</NAME>
<URL>http://www.Microsoft.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c006">
<NAME>Travel</NAME>
<TOP>370</TOP>
<LEFT>480</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>200</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Southwest</NAME>
<URL>http://www.mfa.org</URL>
</URL>
<URL ID="U002">
<NAME>Northwest</NAME>
<URL>http://www.bing.com</URL>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
我还尝试使用 xpath 访问内部节点,但返回了错误。
$urls = $xml->xpath('/COMMUNITIES/COMMUNITY/URLS/URL');
访问内部节点的正确方法是什么?谢谢。
In this php script which reads an XML file, the inner loop for accessing the nested nodes doesn't return any data.
PHP script
<?php
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);
echo <<<EOF
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Top</th>
<th>Left</th>
<th>Width</th>
<th>Height</th>
<th>Urls</th>
</tr>
EOF;
foreach($nodes as $node) // loop through
{
echo "<tr>";
echo " <td>".$node['ID']."</td>";
echo " <td>".$node->NAME."</td>";
echo " <td>".$node->TOP."</td>";
echo " <td>".$node->LEFT."</td>";
echo " <td>".$node->WIDTH."</td>";
echo " <td>".$node->HEIGHT."</td>";
echo " <td>";
$urls = $node->URLS;
/* $urls = $xml->xpath('/COMMUNITIES/COMMUNITY/URLS/URL'); */
foreach($urls as $url)
{
echo ' '.$url->NAME;
}
echo ' </td>';
echo '</tr>';
}
echo '</table>';
?>
XML file
<?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>
<COMMUNITY ID="c002">
<NAME>Local Stores</NAME>
<TOP>50</TOP>
<LEFT>260</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Walgreens</NAME>
<URL>http://www.walgreens.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>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c003">
<NAME>Attractions</NAME>
<TOP>50</TOP>
<LEFT>470</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>300</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Museum</NAME>
<URL>http://www.mfa.org</URL>
</URL>
<URL ID="U002">
<NAME>Park</NAME>
<URL>http://www.bing.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c004">
<NAME>Online Stores</NAME>
<TOP>370</TOP>
<LEFT>50</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>150</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Amazon.com</NAME>
<URL>http://www.amazon.com</URL>
</URL>
<URL ID="U002">
<NAME>Target.com</NAME>
<URL>http://www.target.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c005">
<NAME>Online Forums</NAME>
<TOP>370</TOP>
<LEFT>300</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>200</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Technet</NAME>
<URL>http://www.Microsoft.com</URL>
</URL>
<URL ID="U002">
<NAME>MSDN</NAME>
<URL>http://www.Microsoft.com</URL>
</URL>
</URLS>
</COMMUNITY>
<COMMUNITY ID="c006">
<NAME>Travel</NAME>
<TOP>370</TOP>
<LEFT>480</LEFT>
<WIDTH>200</WIDTH>
<HEIGHT>200</HEIGHT>
<URLS>
<URL ID="U001">
<NAME>Southwest</NAME>
<URL>http://www.mfa.org</URL>
</URL>
<URL ID="U002">
<NAME>Northwest</NAME>
<URL>http://www.bing.com</URL>
</URL>
</URLS>
</COMMUNITY>
</COMMUNITIES>
I also tried accessing the inner node with xpath and that returned errors.
$urls = $xml->xpath('/COMMUNITIES/COMMUNITY/URLS/URL');
What is the right way of accessing the inner nodes? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要将其更改为:
You just need to change it to: