john.muckley Posted January 24, 2014 Share Posted January 24, 2014 Hey guys, Hoping someone can help me out. I have this XML output from a cloud management API which looks like this: <listcapacityresponse cloud-stack-version="4.2.1"><count>6</count> <capacity> <type>8</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>6</capacityused><capacitytotal>18</capacitytotal><percentused>33.33</percentused> </capacity> <capacity> <type>0</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>27246198784</capacityused><capacitytotal>97078222080</capacitytotal><percentused>28.07</percentused> </capacity> <capacity> <type>1</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>13500</capacityused><capacitytotal>52800</capacitytotal><percentused>25.57</percentused> </capacity> <capacity> <type>5</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>3</capacityused><capacitytotal>12</capacitytotal><percentused>25</percentused> </capacity> <capacity> <type>6</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>14619246592</capacityused><capacitytotal>106308304896</capacitytotal><percentused>13.75</percentused> </capacity> <capacity> <type>3</type><zoneid>f26c2094-f2ca-4951-9265-a3f036e7f045</zoneid><zonename>DBXCP1</zonename><capacityused>92190801920</capacityused><capacitytotal>1099511627776</capacitytotal><percentused>8.38</percentused> </capacity> </listcapacityresponse> Each 'type' represents a different available resource, so like type 1 is available CPU, type 0 is available RAM. That sort of thing. I need to be able to parse this so i can split up the individual resources and eventually display their data as a chart. I can do the chart bit, but i need to split the data up first. Any help is appriciated. Thanks Jonny Quote Link to comment https://forums.phpfreaks.com/topic/285637-help-with-simplexml/ Share on other sites More sharing options...
Barand Posted January 24, 2014 Share Posted January 24, 2014 this will put the data in an array $xml = simplexml_load_string($str); foreach ($xml->listcapacityresponse->capacity as $cap) { $results[] = (array)$cap; } echo '<pre>',print_r($results, true),'</pre>'; /* RESULTS *********************************************************** Array ( [0] => Array ( [type] => 8 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 6 [capacitytotal] => 18 [percentused] => 33.33 ) [1] => Array ( [type] => 0 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 27246198784 [capacitytotal] => 97078222080 [percentused] => 28.07 ) [2] => Array ( [type] => 1 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 13500 [capacitytotal] => 52800 [percentused] => 25.57 ) [3] => Array ( [type] => 5 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 3 [capacitytotal] => 12 [percentused] => 25 ) [4] => Array ( [type] => 6 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 14619246592 [capacitytotal] => 106308304896 [percentused] => 13.75 ) [5] => Array ( [type] => 3 [zoneid] => f26c2094-f2ca-4951-9265-a3f036e7f045 [zonename] => DBXCP1 [capacityused] => 92190801920 [capacitytotal] => 1099511627776 [percentused] => 8.38 ) ) *********************************************************************/ Quote Link to comment https://forums.phpfreaks.com/topic/285637-help-with-simplexml/#findComment-1466381 Share on other sites More sharing options...
john.muckley Posted January 24, 2014 Author Share Posted January 24, 2014 Thanks barand! I implemented this like so... <?php $types = simplexml_load_file("data.xml"); $xml = simplexml_load_string($types); foreach ($xml->listcapacityresponse->capacity as $cap) { $results[] = (array)$cap; } echo '<pre>',print_r($results, true),'</pre>'; ?> ...but it doesnt seem to be working. I get no output at all, any idea where i'm going wrong? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/285637-help-with-simplexml/#findComment-1466387 Share on other sites More sharing options...
Barand Posted January 24, 2014 Share Posted January 24, 2014 Either load from string or load from file - not both! <?php $types = simplexml_load_file("data.xml"); foreach ($types->listcapacityresponse->capacity as $cap) { $results[] = (array)$cap; } echo '<pre>',print_r($results, true),'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/285637-help-with-simplexml/#findComment-1466391 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.