我有一个像这样的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>
答案 0 :(得分: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;
}
答案 1 :(得分: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;