简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM