-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)

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
user1546642
  • 19
  • 1
  • 6

1 Answers1

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

MaK
  • 596
  • 4
  • 23
  • I tryed your code: ` ` but my array displys: Array ( [0] => ) What am I doing wrong here? – user1546642 Jul 31 '14 at 11:19
  • $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 – MaK Jul 31 '14 at 11:39
  • It works kastor mania! Thanks! Now I need to get my head around expressing the value "0" from device with id=13 – user1546642 Jul 31 '14 at 11:57