开发者

PHP DOMDocument getting element data with particular attribute name

The XML

<开发者_开发百科;?xml version="1.0"?>
<items version="1.5">
    <item name="device">iphone</item>
    <item name="affinity">testing</item>
</items>

I need to get the value 'iphone' and value 'testing' from device and affinity, how do I select them?

I've tried:

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

But that doesn't seem to work.

Thank you!


Something like this may do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜