2011-05-06 40 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

相关问题