Booster77 Posted December 28, 2013 Share Posted December 28, 2013 Hello everyone, For an application I'm working on I'm connecting to a webservice which gives me an XML as response. I have no influence on how that XML is formatted and I am trying to figure out how to parse this result. The XML I get back is (truncated because it's way too long): <browse result="1" first="1" last="65" total="65"> <th> <td label="dimension" hideforuser="false" type="String">fin.trs.line.dim2</td> <td label="Outstanding" hideforuser="false" type="Value">fin.trs.line.openbasevaluesigned</td> <td label="Factuurbedrag" hideforuser="false" type="Value">fin.trs.line.basevaluesigned</td> <td label="Invoice Number" hideforuser="false" type="String">fin.trs.line.invnumber</td> <td label="" hideforuser="false" type="String">fin.trs.head.code</td> <td label="Pay date" hideforuser="false" type="Date">fin.trs.line.matchdate</td> <td label="Vervaldatum" hideforuser="false" type="Date">fin.trs.line.datedue</td> <td label="Datum" hideforuser="false" type="Date">fin.trs.head.date</td> <td label="boektype" hideforuser="false" type="String">fin.trs.head.status</td> <td label="paystatus" hideforuser="false" type="String">fin.trs.line.availableforpayruns</td> </th> <tr> <td field="fin.trs.line.dim2" hideforuser="false" type="String">2001</td> <td field="fin.trs.line.openbasevaluesigned" hideforuser="false" type="Value">-629.93</td> <td field="fin.trs.line.basevaluesigned" hideforuser="false" type="Value">-629.93</td> <td field="fin.trs.line.invnumber" hideforuser="false" type="String"/> <td field="fin.trs.head.code" hideforuser="false" type="String">BEGINBALANS</td> <td field="fin.trs.line.matchdate" hideforuser="false" type="Date"/> <td field="fin.trs.line.datedue" hideforuser="false" type="Date" name="31/10/2011">20111031</td> <td field="fin.trs.head.date" hideforuser="false" type="Date" name="01/10/2011">20111001</td> <td field="fin.trs.head.status" hideforuser="false" type="String" name="Definitief">final</td> <td field="fin.trs.line.availableforpayruns" hideforuser="false" type="String" name="Ja">true</td> <key> <office>NLA003802</office> <code>BEGINBALANS</code> <number>201100001</number> <line>16</line> </key> </tr> <tr> <td field="fin.trs.line.dim2" hideforuser="false" type="String">2000</td> <td field="fin.trs.line.openbasevaluesigned" hideforuser="false" type="Value">-3570.00</td> <td field="fin.trs.line.basevaluesigned" hideforuser="false" type="Value">-3570.00</td> <td field="fin.trs.line.invnumber" hideforuser="false" type="String"/> <td field="fin.trs.head.code" hideforuser="false" type="String">BEGINBALANS</td> <td field="fin.trs.line.matchdate" hideforuser="false" type="Date"/> <td field="fin.trs.line.datedue" hideforuser="false" type="Date" name="31/10/2011">20111031</td> <td field="fin.trs.head.date" hideforuser="false" type="Date" name="01/10/2011">20111001</td> <td field="fin.trs.head.status" hideforuser="false" type="String" name="Definitief">final</td> <td field="fin.trs.line.availableforpayruns" hideforuser="false" type="String" name="Ja">true</td> <key> <office>NLA003802</office> <code>BEGINBALANS</code> <number>201100001</number> <line>17</line> </key> </tr> After I've done simplexml_load_string($result->ProcessXmlStringResult) it turns into: SimpleXMLElement Object ( [@attributes] => Array ( [result] => 1 [first] => 1 [last] => 65 [total] => 65 ) [th] => SimpleXMLElement Object ( [td] => Array ( [0] => fin.trs.line.dim2 [1] => fin.trs.line.openbasevaluesigned [2] => fin.trs.line.basevaluesigned [3] => fin.trs.line.invnumber [4] => fin.trs.head.code [5] => fin.trs.line.matchdate [6] => fin.trs.line.datedue [7] => fin.trs.head.date [8] => fin.trs.head.status [9] => fin.trs.line.availableforpayruns ) ) [tr] => Array ( [0] => SimpleXMLElement Object ( [td] => Array ( [0] => 2001 [1] => -629.93 [2] => -629.93 [3] => SimpleXMLElement Object ( [@attributes] => Array ( [field] => fin.trs.line.invnumber [hideforuser] => false [type] => String ) ) [4] => BEGINBALANS [5] => SimpleXMLElement Object ( [@attributes] => Array ( [field] => fin.trs.line.matchdate [hideforuser] => false [type] => Date ) ) [6] => 20111031 [7] => 20111001 [8] => final [9] => true ) [key] => SimpleXMLElement Object ( [office] => NLA003802 [code] => BEGINBALANS [number] => 201100001 [line] => 16 ) ) [1] => SimpleXMLElement Object ( [td] => Array ( [0] => 2000 [1] => -3570.00 [2] => -3570.00 [3] => SimpleXMLElement Object ( [@attributes] => Array ( [field] => fin.trs.line.invnumber [hideforuser] => false [type] => String ) ) [4] => BEGINBALANS [5] => SimpleXMLElement Object ( [@attributes] => Array ( [field] => fin.trs.line.matchdate [hideforuser] => false [type] => Date ) ) [6] => 20111031 [7] => 20111001 [8] => final [9] => true ) [key] = I don't know the following things: 1. Am I doing a smart move by doing the simplexml_load_string in the first place? Or should I just 'parse' the XML I get in return? The XML is a little complicated to me because it makes use of the '<td field="name">value here</td> construct. 2. How do I get the field name and the corresponding value out of the XML or the array? What I need is the names and values in the <td> elements under <tr> and the values under <key> I've been looking for a way for 2 days now and I'm hoping very much you can help. If you can get me started that would be great. Thank you so much in advance. Quote Link to comment https://forums.phpfreaks.com/topic/284949-how-to-parse-this-xml-array/ Share on other sites More sharing options...
Solution Barand Posted December 28, 2013 Solution Share Posted December 28, 2013 try $xml = simplexml_load_string($str); #echo '<pre>',print_r($xml, true),'</pre>'; foreach ($xml->tr as $tr) { echo "<h4>td</h4>"; foreach ($tr->td as $td) { echo "{$td['field']} : $td<br>"; } echo "<h4>key</h4>"; echo "office: $tr->key->office<br>"; echo "code: $tr->key->code<br>"; echo "number: $tr->key->number<br>"; echo "line: $tr->key->line<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/284949-how-to-parse-this-xml-array/#findComment-1463181 Share on other sites More sharing options...
Booster77 Posted December 29, 2013 Author Share Posted December 29, 2013 (edited) Hi Barand, That was the solution! Thank you so much for making me understand! Edited December 29, 2013 by Booster77 Quote Link to comment https://forums.phpfreaks.com/topic/284949-how-to-parse-this-xml-array/#findComment-1463210 Share on other sites More sharing options...
Barand Posted December 29, 2013 Share Posted December 29, 2013 The code I posted almost worked for the extract you provided (with the addition of </browse> at the end. I have since added {...} to fix the bug when printing the <key> values $xml = simplexml_load_string($str); #echo '<pre>',print_r($xml, true),'</pre>'; foreach ($xml->tr as $tr) { echo "<h4>td</h4>"; foreach ($tr->td as $td) { echo "{$td['field']} : $td<br>"; } echo "<h4>key</h4>"; echo "office: {$tr->key->office}<br>"; echo "code: {$tr->key->code}<br>"; echo "number: {$tr->key->number}<br>"; echo "line: {$tr->key->line}<br>"; } /*** RESULTS *********************************************** td fin.trs.line.dim2 : 2001 fin.trs.line.openbasevaluesigned : -629.93 fin.trs.line.basevaluesigned : -629.93 fin.trs.line.invnumber : fin.trs.head.code : BEGINBALANS fin.trs.line.matchdate : fin.trs.line.datedue : 20111031 fin.trs.head.date : 20111001 fin.trs.head.status : final fin.trs.line.availableforpayruns : true key office: NLA003802 code: BEGINBALANS number: 201100001 line: 16 td fin.trs.line.dim2 : 2000 fin.trs.line.openbasevaluesigned : -3570.00 fin.trs.line.basevaluesigned : -3570.00 fin.trs.line.invnumber : fin.trs.head.code : BEGINBALANS fin.trs.line.matchdate : fin.trs.line.datedue : 20111031 fin.trs.head.date : 20111001 fin.trs.head.status : final fin.trs.line.availableforpayruns : true key office: NLA003802 code: BEGINBALANS number: 201100001 line: 17 ****************************************************************/ It depends on how much pruning you did before posting. If the <browse> tag is embedded inside other tags you need to adjust to take the parent(s) into account, but I cannot to more without the full xml structure. Quote Link to comment https://forums.phpfreaks.com/topic/284949-how-to-parse-this-xml-array/#findComment-1463211 Share on other sites More sharing options...
Booster77 Posted December 29, 2013 Author Share Posted December 29, 2013 The code I posted almost worked for the extract you provided (with the addition of </browse> at the end. I have since added {...} to fix the bug when printing the <key> values $xml = simplexml_load_string($str); #echo '<pre>',print_r($xml, true),'</pre>'; foreach ($xml->tr as $tr) { echo "<h4>td</h4>"; foreach ($tr->td as $td) { echo "{$td['field']} : $td<br>"; } echo "<h4>key</h4>"; echo "office: {$tr->key->office}<br>"; echo "code: {$tr->key->code}<br>"; echo "number: {$tr->key->number}<br>"; echo "line: {$tr->key->line}<br>"; } /*** RESULTS *********************************************** td fin.trs.line.dim2 : 2001 fin.trs.line.openbasevaluesigned : -629.93 fin.trs.line.basevaluesigned : -629.93 fin.trs.line.invnumber : fin.trs.head.code : BEGINBALANS fin.trs.line.matchdate : fin.trs.line.datedue : 20111031 fin.trs.head.date : 20111001 fin.trs.head.status : final fin.trs.line.availableforpayruns : true key office: NLA003802 code: BEGINBALANS number: 201100001 line: 16 td fin.trs.line.dim2 : 2000 fin.trs.line.openbasevaluesigned : -3570.00 fin.trs.line.basevaluesigned : -3570.00 fin.trs.line.invnumber : fin.trs.head.code : BEGINBALANS fin.trs.line.matchdate : fin.trs.line.datedue : 20111031 fin.trs.head.date : 20111001 fin.trs.head.status : final fin.trs.line.availableforpayruns : true key office: NLA003802 code: BEGINBALANS number: 201100001 line: 17 ****************************************************************/ It depends on how much pruning you did before posting. If the <browse> tag is embedded inside other tags you need to adjust to take the parent(s) into account, but I cannot to more without the full xml structure. I get what you mean It worked right away tho. I'm very happy with this. Now I understand the way it's supposed to be done I can continue. Once again thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/284949-how-to-parse-this-xml-array/#findComment-1463220 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.