我有這個下面的XML文件: -多個節點
<item>
<title>Troggs singer Reg Presley dies at 71</title>
<description>Reg Presley, the lead singer of British rock band The Troggs, whose hits in the 1960s included Wild Thing, has died aged 71.</description>
<link>http://www.bbc.co.uk/news/uk-21332048#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/uk-21332048</guid>
<pubDate>Tue, 05 Feb 2013 01:13:07 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65701000/jpg/_65701366_65701359.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65701000/jpg/_65701387_65701359.jpg"/>
</item>
<item>
<title>Horsemeat found at Newry cold store</title>
<description>Horse DNA has been found in frozen meat in a cold store in Northern Ireland, as Irish police investigate a third case of contamination.</description>
<link>http://www.bbc.co.uk/news/world-europe-21331208#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/world-europe-21331208</guid>
<pubDate>Mon, 04 Feb 2013 23:47:38 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700000_002950295-1.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65700000/jpg/_65700001_002950295-1.jpg"/>
</item>
<item>
<title>US 'will sue' Standard & Poor's</title>
<description>Standard & Poor's says it is to be sued by the US government over the credit ratings agency's assessment of mortgage bonds before the financial crisis.</description>
<link>http://www.bbc.co.uk/news/21331018#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/21331018</guid>
<pubDate>Mon, 04 Feb 2013 22:45:52 GMT</pubDate>
<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/65701000/jpg/_65701717_mediaitem65699884.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/65701000/jpg/_65701718_mediaitem65699884.jpg"/>
</item>
現在,當我給輸入節點爲「項目」來獲取數據,比,而不是顯示所有的項目節點它只顯示最後一個項目節點.....
我的代碼是: -
$dom->load($url);
$link = $dom->getElementsByTagName($tag_name);
$value = array();
for ($i = 0; $i < $link->length; $i++) {
$childnode['name'] = $link->item($i)->nodeName;
$childnode['value'] = $link->item($i)->nodeValue;
$value[$childnode['name']] = $childnode['value'];
}
這裏,$網址是我的XML頁面 $ TAG_NAME的網址是節點的名稱,在此它是「項目」
我所得到的輸出是: -
US 'will sue' Standard & Poor's.Standard & Poor's says it is to be sued by the US government over the credit ratings agency's assessment of mortgage bonds before the financial crisis.http://www.bbc.co.uk/news/21331018#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa.http://www.bbc.co.uk/news/world-europe-21331208.Mon, 04 Feb 2013 22:45:52 GMT
這是最後一個標籤的數據。我希望所有的商品標籤的數據,也是我想要的數據,在此格式: -
title :- US 'will sue' Standard & Poor's
description :- Standard & Poor's says it is to be sued by the US government over
the credit ratings agency's assessment of mortgage bonds before the financial crisis
我想要的childNodes的連名字(如果有的話)我的輸出... 請幫我出來....
感謝確實一個很好的解釋....... –
不客氣。快樂的編碼! – thaJeztah