从 simplexml_load_file 获取属性
Getting attribute from simplexml_load_file
我正在尝试找出如何从以下 XML.
加载变量位置、时间和值
XML 看起来像这样:
<root xmlns="">
<sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/>
<status level="2" location="AAA" time="03/21/2018 14:09:08"/>
</root>
解析后的 XML 看起来像这样:
object(SimpleXMLElement)#1 (2) {
["sns"]=>
object(SimpleXMLElement)#2 (1) {
["@attributes"]=>
array(8) {
["id"]=>
string(1) "1"
["name"]=>
string(8) "Senzor A"
["type"]=>
string(1) "1"
["status"]=>
string(1) "0"
["unit"]=>
string(1) "0"
["val"]=>
string(3) "4.5"
["w-min"]=>
string(0) ""
["w-max"]=>
string(0) ""
}
}
["status"]=>
object(SimpleXMLElement)#3 (1) {
["@attributes"]=>
array(3) {
["level"]=>
string(1) "2"
["location"]=>
string(3) "AAA"
["time"]=>
string(19) "03/21/2018 14:09:08"
}
}
}
我很难弄清楚如何导航,所以如果有人能给我一些建议,我将不胜感激。
可以使用 $tag['attribute_name']
.
使用 simpleXML 访问属性
可以使用 $xml->tag
或 $xml->tag->subtag
来访问元素。
$xml = '<root xmlns="">
<sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/>
<status level="2" location="AAA" time="03/21/2018 14:09:08"/>
</root>';
$xml = simplexml_load_string($xml); // or simplexml_load_file("file.xml");
echo $xml->status['location'];
echo $xml->status['time'];
echo $xml->sns['val'];
输出:
AAA
03/21/2018 14:09:08
4.5
我正在尝试找出如何从以下 XML.
加载变量位置、时间和值XML 看起来像这样:
<root xmlns="">
<sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/>
<status level="2" location="AAA" time="03/21/2018 14:09:08"/>
</root>
解析后的 XML 看起来像这样:
object(SimpleXMLElement)#1 (2) {
["sns"]=>
object(SimpleXMLElement)#2 (1) {
["@attributes"]=>
array(8) {
["id"]=>
string(1) "1"
["name"]=>
string(8) "Senzor A"
["type"]=>
string(1) "1"
["status"]=>
string(1) "0"
["unit"]=>
string(1) "0"
["val"]=>
string(3) "4.5"
["w-min"]=>
string(0) ""
["w-max"]=>
string(0) ""
}
}
["status"]=>
object(SimpleXMLElement)#3 (1) {
["@attributes"]=>
array(3) {
["level"]=>
string(1) "2"
["location"]=>
string(3) "AAA"
["time"]=>
string(19) "03/21/2018 14:09:08"
}
}
}
我很难弄清楚如何导航,所以如果有人能给我一些建议,我将不胜感激。
可以使用 $tag['attribute_name']
.
可以使用 $xml->tag
或 $xml->tag->subtag
来访问元素。
$xml = '<root xmlns="">
<sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/>
<status level="2" location="AAA" time="03/21/2018 14:09:08"/>
</root>';
$xml = simplexml_load_string($xml); // or simplexml_load_file("file.xml");
echo $xml->status['location'];
echo $xml->status['time'];
echo $xml->sns['val'];
输出:
AAA
03/21/2018 14:09:08
4.5