我在这里停留了一段时间。我使用 getElementsByTagName 在 xml 中获取节点值。但是当节点有子节点时我面临一个问题。现在,当我使用标签 getElementsByTagName 时,我什至想打印子节点及其值。
ex:-
<data>
<details>SOme details </details>
<item>
<title>Top Tories urge gay marriage support</title>
<description>Three senior Conservative ministers urge party MPs to drop any opposition to allowing gay marriage in England and Wales, ahead of a Commons vote.</description>
<link>http://www.bbc.co.uk/news/uk-politics-21325702#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/uk-politics-21325702</guid>
<pubDate>Tue, 05 Feb 2013 05:04:20 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702114_65557953.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65702000/jpg/_65702115_65557953.jpg"/>
</item>
<item>
<title>'Independence day' plans revealed</title>
<description>The Scottish government has drawn up detailed plans for a transition to independence in March 2016 if there is a "yes" vote, the BBC can reveal.</description>
<link>http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/uk-scotland-scotland-politics-21331302</guid>
<pubDate>Tue, 05 Feb 2013 05:31:51 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700866_013673187-1.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700988_013673187-1.jpg"/>
</item>
<item>
<title>Reconstructed face of Richard III</title>
<description>A facial reconstruction of how Richard III may have looked is released, after archaeologists confirmed a skeleton found beneath a Leicester car park was that of the English king.</description>
<link>http://www.bbc.co.uk/news/uk-england-leicestershire-21328380#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/uk-england-leicestershire-21328380</guid>
<pubDate>Mon, 04 Feb 2013 23:03:16 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701188_richardiii.png"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65701000/png/_65701189_richardiii.png"/>
</item>
</data>
我得到的输出为:-
somedata1somedata2somedata3somedata4.
我想要输出为: -
subnode1:-somedata1
subnode2:-somedata2
subnode3:-somedata3
subnode4:-somedata4
我的代码是:-
$dom->load($url);//url of the xml file
$link = $dom->getElementsByTagName($tag_name);//tag_name is the node name
$value = array();
for ($i = 0; $i < $link->length; $i++) {
$childnode['name'] = $link->item($i)->nodeName;
$childnode['value'] = $link->item($i)->nodeValue;
echo "Name : " . $childnode['name'];
echo "<br /> Value : " . $childnode['value'];
$value[$childnode['name']] = $childnode['value'];
}
print_r($value);