Hi
I am trying to retrieve values from a retrieved XML file after making a POST webservice call. The structure of the returned XML file is -
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CalculateRateResponse xmlns="
http://external website address">
<CalculateRateResult><Connote Entity="PK0"><_currency>£</_currency><accno>123456</accno><carrierid>4</carrierid><carriername>Test Carrier1</carriername><carrzone>ZONE 1</carrzone><chgwgt>5</chgwgt><condate>31/03/2021</condate><currency>GBP</currency><datelong>20210331</datelong><deliverytimeraw>[none]</deliverytimeraw><freightlinedetails><amt>10</amt><cube>0.1</cube><desc>Piece</desc><ref>101002</ref><wgt>5</wgt><youritemdesc>MEXICO Swivel Chair, black</youritemdesc></freightlinedetails><fromzone>ZONE 1</fromzone><grandtotmrkup>0.00</grandtotmrkup><grandtotpostpickuprate>0.00</grandtotpostpickuprate><grandtotrate>0.00</grandtotrate><gstamount>0.00</gstamount><gstonmrkup>0.00</gstonmrkup><mrkupchrg>0.00</mrkupchrg><postpickupgst>0.00</postpickupgst><rate>0.00</rate><rateerror>An Applicable rate could not be found!</rateerror><rateerrorgeoloc>Unable to determine Geo Location</rateerrorgeoloc><recaccno>TEST</recaccno><recaddr><add1>Station Road, 21</add1><add3>Cambridge</add3><add4>Cambridgeshire</add4><add5>CB1 2FB</add5><add6>UNITED KINGDOM</add6></recaddr><recloc>0,0</recloc><recname>TEST RECEIVER</recname><rtzexpire>999912312359</rtzexpire><sendaddr><add1>7122 South Ashford Street</add1><add3>London</add3><add4>Greater London</add4><add5>W2 8HG</add5><add6>UNITED KINGDOM</add6></sendaddr><sendloc>0,0</sendloc><sendname>SENDER NAME</sendname><service>PARCEL</service><taxrate>20</taxrate><totalcost>0.00</totalcost><totcostplusmrkup>0.00</totcostplusmrkup><totcube>0.1</totcube><totdgcharge>0.00</totdgcharge><totitems>10</totitems><totlevy>0.00</totlevy><totpostpickuprecovery>0.00</totpostpickuprecovery><totsurchrg>0.00</totsurchrg><totwgt>5</totwgt></Connote></CalculateRateResult>
</CalculateRateResponse>
</soap:Body>
</soap:Envelope>
From the returned file I would like to extract certain values such as carriername (Test Carrier1) however struggling. Please could you advise on what the best approach would be to extract fields from a file with a structure like the above
Thanking you all in advance for any assistance.
Regards
Gurn
Answers
Result := CallWebServiceCalcRate('CalculateRate', Parameters);
Parameters is the XML document being passed into the webservice call with Result being the XML document being returned.
TotalCost := GetField(Result, 'Body/CalculateRateResponse/CalculateRateResult/totalcost');
GetField function created to locate the totalcost value (<totalcost>0.00</totalcost> from above file structure)
local procedure GetField(var XmlD: XmlDocument; Path: text): text
var
V: Variant;
N: XMLNode;
begin
if XmlD.SelectSingleNode(Path, N) then
exit(N.AsXmlElement().InnerText);
end;
However the function is returning blank as it fails to process the exit action so it is not finding the node. Could this be anything to do with not removing the soapenv:Envelope before trying to locate node. if so how do i remove this.
https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/350713/how-to-parse-xml-with-xpath-and-namespace/937072