2013-02-05 72 views
2

我有這個下面的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&amp;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&amp;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 &amp; Poor's</title> 
    <description>Standard &amp; 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&amp;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 &amp; Poor's.Standard &amp; 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&amp;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 &amp; Poor's 
description :- Standard &amp; 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的連名字(如果有的話)我的輸出... 請幫我出來....

回答

2

你似乎遍歷的「項目」節點只和其他人所說,是在覆蓋以前的值每次迭代。

如果你使用print_r($ value)裏面調試$ value數組的循環;

$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']; 

    echo 'iteration: ' . $i . '<br />'; 
    echo '<pre>'; print_r($value); echo '</pre>'; 
} 

你可能會看到這樣的事情

// iteration: 0 
Array 
(
    [item] => Troggs singer Reg Presley dies at 71 ...... 
) 

// iteration: 1 
Array 
(
    [item] => Horsemeat found at Newry cold store ......... 
) 

// iteration: 2 
Array 
(
    [item] => US 'will sue' Standard & Poor's ......... 
) 

什麼你應該做的是:

$dom = new DOMDocument(); 
$dom->preserveWhiteSpace = false; 
$dom->load($url); 
$items = $dom->getElementsByTagName($tag_name); 
$values = array(); 

foreach ($items as $item) { 
    $itemProperties = array(); 

    // Loop through the 'sub' items 
    foreach ($item->childNodes as $child) { 
     // Note: using 'localName' to remove the namespace 
     if (isset($itemProperties[(string) $child->localName])) { 
      // Quickfix to support multiple 'thumbnails' per item (although they have no content) 
      $itemProperties[$child->localName] = (array) $itemProperties[$child->localName]; 
      $itemProperties[$child->localName][] = $child->nodeValue; 
     } else { 
      $itemProperties[$child->localName] = $child->nodeValue; 
     } 
    } 

    // Append the item to the 'values' array 
    $values[] = $itemProperties; 

} 


// Output the result 
echo '<pre>'; print_r($values); echo '</pre>'; 

,輸出:

Array 
(
    [0] => Array 
     (
      [title] => Troggs singer Reg Presley dies at 71 
      [description] => Reg Presley, the lead singer of British rock band The Troggs, whose hits in the 1960s included Wild Thing, has died aged 71. 
      [link] => http://www.bbc.co.uk/news/uk-21332048#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
      [guid] => http://www.bbc.co.uk/news/uk-21332048 
      [pubDate] => Tue, 05 Feb 2013 01:13:07 GMT 
      [thumbnail] => Array 
       (
        [0] => 
        [1] => 
       ) 

     ) 

    [1] => Array 
     (
      [title] => Horsemeat found at Newry cold store 
      [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. 
      [link] => http://www.bbc.co.uk/news/world-europe-21331208#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
      [guid] => http://www.bbc.co.uk/news/world-europe-21331208 
      [pubDate] => Mon, 04 Feb 2013 23:47:38 GMT 
      [thumbnail] => Array 
       (
        [0] => 
        [1] => 
       ) 

     ) 

    [2] => Array 
     (
      [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. 
      [link] => http://www.bbc.co.uk/news/21331018#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa 
      [guid] => http://www.bbc.co.uk/news/21331018 
      [pubDate] => Mon, 04 Feb 2013 22:45:52 GMT 
      [thumbnail] => Array 
       (
        [0] => 
        [1] => 
       ) 

     ) 

) 
+0

感謝確實一個很好的解釋....... –

+1

不客氣。快樂的編碼! – thaJeztah

0

我覺得代碼有問題:

for ($i = 0; $i < $link->length; $i++) { 
     $childnode['name'] = $link->item($i)->nodeName; 
     $childnode['value'] = $link->item($i)->nodeValue; 
     $value[$childnode['name']] = $childnode['value']; 
    } 

通過for loop並在最後用新的價值分配每次$childnode['name']$i等於的$link.length長度那麼該值將阿西導致$childnode array。 因此減少的問題應該是一個多維數組一樣

for ($i = 0; $i < $link->length; $i++) { 
    $childnode['name'][$i] = $link->item($i)->nodeName; 
    $childnode['value'][$i] = $link->item($i)->nodeValue; 
    $value[$childnode['name'][$i]][$i] = $childnode['value']; 
} 

爲了測試它:print_r($childnode);

+0

不工作的傢伙,我得到相同的結果 –

+0

我無法識別fy子節點... –

+1

製作一個節點,例如'root'的主要元素,然後將其添加到您的代碼中'$ dom-> load($ url); \t $ root = $ dom-> documentElement; $ link = $ root-> getElementsByTagName($ tag_name);' –

1

你的問題是,你的源XML需要有一個根節點(也可以叫任何你想要的)。要成爲有效的XML,您始終需要一個根節點。也就是說,每個有效的XML文件都只有一個沒有父節點或兄弟節點的元素。一旦你有了根節點,你的XML就會加載到你的對象中。

例如:

<root> 
    <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&amp;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&amp;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 &amp; Poor's</title> 
     <description>Standard &amp; 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&amp;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> 
</root> 
+0

仍然無法正常工作........ –

2

(不要忘了根節點。)它看起來像的方法之一是剛剛連接所有的文本節點的那個元素在一起(差不多相當於一個XSL :select的值=。)。我從來沒有在PHP中使用DOMDocument類和相關類。但是你可以做的是使用C14N()方法對DOMNode進行規範化,然後解析結果字符串。這是不漂亮,但它得到你想要的結果,並且易於擴展:

$tag_name = 'item'; 
    $link = $dom->getElementsByTagName($tag_name); 
    for ($i = 0; $i < $link->length; $i++) { 
     $treeAsString = $link->item($i)->C14N(); 
     $curBranchParts = explode("\n",$treeAsString); 
     $curBranchPartsSize = count($curBranchParts); 
     $curBranchParts = explode("\n",$treeAsString); 
     $curBranchPartsSize = count($curBranchParts); 
     for ($j = 1; $j < ($curBranchPartsSize - 1); $j++) { 
      $curItem = $curBranchParts[$j]; 
      $curItemParts = explode('<', $curItem); 
      $tagWithContent = $curItemParts[1]; 
      $tagWithContentParts = explode('>',$tagWithContent); 
      $tag = $tagWithContentParts[0]; 
      $content = $tagWithContentParts[1]; 

      if (trim($content) != '') echo $tag . ' :- ' . $content . '<br />'; 
      else echo $tag . '<br />'; 
     } 
    } 
+0

感謝代碼////// –