2011-05-06 34 views
1

的XML具有特定屬性PHP DOM文檔獲取元數據名稱

<?xml version="1.0"?> 
<items version="1.5"> 
    <item name="device">iphone</item> 
    <item name="affinity">testing</item> 
</items> 

我需要從設備和親和力值「iPhone」和值「測試」,我該如何選擇呢?

我已經試過:

$xml  = new DOMDocument(); 
$xml->loadXML($request); 
$itemList = $xml->getElementsByTagName('item'); 
foreach($itemList as $install) 
{ 
    echo $install->getAttribute('device')->item(0)->nodeValue;   
} 

但是,這似乎並沒有工作。

謝謝!

回答

1

像這樣的東西可以做:

$Device; 
$Affinity; 
foreach($itemList as $install) 
{ 
    if($install->getAttribute('name') =='device'){ 
     $Device = $install->nodeValue; 
    } 
    if($install->getAttribute('name')=='affinity'){ 
     $Affinity = $install->nodeValue; 
    } 
} 
+0

的偉大工程謝謝:d – Titan 2011-05-06 14:27:44

相關問題