2010-12-02 68 views
0

我試圖讓所有類別並將其推入我的array解析DOM文檔,到目前爲止我做這種方式,我的xml每個項目有幾個類別。什麼是這樣做的好方法?RSS在PHP

<item> 
    <title>Submit</title> 
    <category>Foo</category> 
    <category>Bar</category> 
</item> 

感謝

+0

謝謝大家! – JoshReitch 2010-12-02 15:35:58

回答

3

這應有助於:

$doc = new DOMDocument(); 
$doc->load('myxml.xml'); 
$arr = array(); 
foreach ($doc->getElementsByTagName('item') as $node) { 
    $item = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
     'date' => array() 
    ); 

    foreach($node->getElementsByTagName('category') as $catNode) 
    { 
     $item['date'][] = $catNode->nodeValue; 
    } 

    $arr[] = $item; 
} 
  • 基督教
0

您需要嵌套的循環:

$doc = new DOMDocument(); 
$doc->load('myxml.xml'); 
$arr = array(); 

foreach ($doc->getElementsByTagName('item') as $itemNode) { 
    $items = array( 
     'title' => $itemNode->getElementsByTagName('title')->item(0)->nodeValue, 
     'date' => array() 
    ); 

    foreach ($itemNode->getElementsByTagName('category') as $categoryNode) { 
     $items['date'][] = $categoryNode->nodeValue; 
    } 

    $arr[] = $items; 
} 
+0

但是,這是否是arr []的一部分?我試圖將它們推入我的arr [] – JoshReitch 2010-12-02 15:26:11