I'm trying to figure out how to load the variable location,, time and val from the following XML.
The XML looks like this:
<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>
Parsed XML looks like this:
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"
}
}
}
I'm having a hard time figuring out how to navigate, so if anyone could throw me some pointers I'd be grateful.
Accessing attributes with simpleXML can be done using $tag['attribute_name']
.
Accessing elements can be done using $xml->tag
or $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'];
Outputs:
AAA
03/21/2018 14:09:08
4.5