私はしばらくここで立ち往生しています。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);