检查xml中是否存在子节点

时间:2013-02-05 07:00:56

标签: php xml cakephp dom

我被困在这里一段时间。我使用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&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);

1 个答案:

答案 0 :(得分: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>';
    }
}