簡體   English   中英

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”和值“ testing”,如何選擇它們?

我試過了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM