以下の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'];
}
ここで、$url は私の xml ページの URL です $tag_name はノードの名前です。この場合は「item」です
私が得る出力は次のとおりです:-
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
出力に子ノードの名前(ある場合)も必要です...助けてください....