-1

I have php file that extracts the following XML from a web service:

<deviceList>
    <device 
        name="Low volt light" 
        id="10" 
        type="Z-Wave Switch Multilevel" 
        value="0"
        address="00016922-018" 
        code="" 
        canDim="True" 
        lastChange="26-07-2014 17:31:33" 
        firstLocation="Kitchen" 
        secondLocation="Main" 
    />
    <device 
        name="Light" 
        id="11" 
        type="Z-Wave Switch Multilevel" 
        value="0" 
        address="00016922-019" 
        code="" 
        canDim="True" 
        lastChange="28-07-2014 18:27:56" 
        firstLocation="Bedroom" 
        secondLocation="Main" 
    /> 
</deviceList>

To be it seems formatted correctly, but I can not get it loaded into an array or object. I have been through W3School and simliar sources, but even with the examples there, I still cant get i to work. Could someone help me here, and maybe also show how the code should be for searching through the array for "id=10" and then display the corresponding "value" (in this example =0)

1
  • Thou shall not believe in "W3School and simliar sources". Commented Jul 31, 2014 at 10:53

1 Answer 1

0

try this, shoudl work ($xml is your xml string) :

$array = json_decode(json_encode((array)simplexml_load_string($xml)),1);

take a look at this codepad

Sign up to request clarification or add additional context in comments.

3 Comments

I tryed your code: <?php //$feed_url = "http://192.168.1.90/ui1/homeseer_rest_api.aspx?function=listdevices&param1=all"; $feed_url = "demoxml.xml"; $content = file_get_contents($feed_url); $xml = new SimpleXmlElement($content); //$xml = $feed_url; $array = json_decode(json_encode((array)simplexml_load_string($xml)),1); print_r($array); //var_dump($xml) ?> but my array displys: Array ( [0] => ) What am I doing wrong here?
$xml should be the file content try this $xml = file_get_contents($feed_url); and then the code above (in my answer) to generate the array
It works kastor mania! Thanks! Now I need to get my head around expressing the value "0" from device with id=13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.