PHP DOMDocument获取具有特定属性名称的元素数据


PHP DOMDocument getting element data with particular attribute name

XML

<?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;          
}

但这似乎行不通。

谢谢!

可以这样做:

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