0

我有一个这样的 XML 文件,我只想获取属性内的值为“1”,PHP 代码应该是什么?提前致谢。

<?xml version="1.0" encoding="UTF-8" ?> 
<nodeInfo>
<node flag="128">
<address>18 F5 40 1</address> 
<name>iMeterSolo</name> 
<type>9.7.135.92</type> 
<enabled>true</enabled> 
<deviceClass>0</deviceClass> 
<wattage>0</wattage> 
<dcPeriod>0</dcPeriod> 
<pnode>18 F5 40 1</pnode> 
<property id="ST" ***value="1"*** formatted="1" uom="W" /> 
</node>
<properties>
<property id="ST" value="1" formatted="1" uom="W" /> 
<property id="TPW" value="226" formatted="226" uom="kWs" /> 
</properties>
</nodeInfo>
4

2 回答 2

0
$string = '<?xml version="1.0" encoding="UTF-8" ?> 
    <nodeInfo>
        <node flag="128">
            <address>18 F5 40 1</address> 
            <name>iMeterSolo</name> 
            <type>9.7.135.92</type> 
            <enabled>true</enabled> 
            <deviceClass>0</deviceClass> 
            <wattage>0</wattage> 
            <dcPeriod>0</dcPeriod> 
            <pnode>18 F5 40 1</pnode> 
            <property id="ST" value="1" formatted="1" uom="W" /> 
        </node>
        <properties>
           <property id="ST" value="1" formatted="1" uom="W" /> 
           <property id="TPW" value="226" formatted="226" uom="kWs" /> 
        </properties>
</nodeInfo>';

$xml = simplexml_load_string($string);

echo (string) $xml->node->property->attributes()->value.PHP_EOL;

foreach($xml->properties->property as $element) {

    $attr = $element->attributes();

    echo (string) $attr->value.PHP_EOL;

}
于 2012-07-12T06:07:21.867 回答
0

尝试这个:

<?php
    $xml = '<nodeInfo><node flag="128">
    <address>18 F5 40 1</address> 
    <name>iMeterSolo</name> 
    <type>9.7.135.92</type> 
    <enabled>true</enabled> 
    <deviceClass>0</deviceClass> 
    <wattage>0</wattage> 
    <dcPeriod>0</dcPeriod> 
    <pnode>18 F5 40 1</pnode> 
    <property id="ST" value="1" formatted="1" uom="W" /> 
    </node>
    <properties>
    <property id="ST" value="1" formatted="1" uom="W" /> 
    <property id="TPW" value="226" formatted="226" uom="kWs" /> 
    </properties>
    </nodeInfo>';
    $xmlObj = simplexml_load_string($xml);
    $arrXml = array($xmlObj);
    echo $arrXml[0]->node->property->attributes()->value;
于 2012-07-12T06:10:58.513 回答