2013-02-05 53 views
0

我在這裏停留了一段時間。我使用getElementsByTagName在xml中獲取節點值。但是當節點有子節點時,我面臨一個問題。現在當我使用標籤getElementsByTagName時,我想要打印子節點及其值。檢查子節點是否在xml中退出

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&amp;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&amp;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&amp;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); 

回答

2
$dom = new DOMDocument('1.0'); 
$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> 
<node> 
<subnode1>somedata1</subnode1> 
<subnode2>somedata2</subnode2> 
<subnode3>somedata3</subnode3> 
<subnode4>somedata4</subnode4> 
</node>'); 
$node = $dom->getElementsByTagName('node'); 
$subNodes = $node->item(0)->childNodes; 
for ($i = 0; $i < $subNodes->length; $i++) { 
    $subNode = $subNodes->item($i); 
    if ($subNode->nodeType === XML_ELEMENT_NODE) { 
     printf('%s: %s<br>', $subNode->nodeName, $subNode->nodeValue); 
    } 
} 

UPD

$dom = new DOMDocument('1.0'); 
@$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> 
<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&amp;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&amp;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&amp;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>'); 

$xpath = new DOMXPath($dom); 
$details = $xpath->query('//data/details')->item(0)->nodeValue; 
echo "Details: $details<br><br>"; 
$items = $xpath->query('//data/item'); 
foreach ($items as $item) { 
    $subItem = $item->childNodes; 
    for ($i = 0; $i < $subItem->length; $i++) { 
     $current = $subItem->item($i); 
     if ($current->nodeType === XML_ELEMENT_NODE) { //ELEMENT_NODE 
      $name = $current->nodeName; 
      $value = ($current->nodeName !== 'thumbnail') ? $current->nodeValue : $current->getAttribute('url'); 
      printf('%s: %s<br>', $name, $value); 
     } 
    } 
    echo '<br>'; 
} 

UPD#2

$data = $dom->getElementsByTagName('data'); 
$dataChildren = $data->item(0)->childNodes; 
for ($i = 0; $i < $dataChildren->length; $i++) { 
    $child = $dataChildren->item($i); 
    if ($child->nodeName === 'details') { 
     $details = $dataChildren->item($i); 
     printf('%s: %s<br><br>', $details->nodeName, $details->nodeValue); 
    } 
    if ($child->nodeName === 'item') { 
     $itemChildren = $child->childNodes; 
     for ($j = 0; $j < $itemChildren->length; $j++) { 
      $item = $itemChildren->item($j); 
      if ($item->nodeType === XML_ELEMENT_NODE) { 
       $name = $item->nodeName; 
       $value = ($item->nodeName !== 'thumbnail') ? $item->nodeValue : $item->getAttribute('url'); 
       printf('%s: %s<br>', $name, $value); 
      } 
     } 
     echo '<br>'; 
    } 
} 
+1

+1大回​​答 – tnanoba

+0

那真棒,非常感謝..............一個問題, .dude如果我們沒有子節點它打印空,如果我們有多個同名節點它不工作..... –

+0

+1它的確很棒的回答 –

相關問題