简体   繁体   English

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

[英]PHP DOMDocument getting element data with particular attribute name

The XML 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? 我需要从设备和关联性中获取值“ iphone”和值“ testing”,如何选择它们?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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