Retrieve value from XML file with <

Gurn69
Member Posts: 10
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
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
0
Answers
-
If it's valid XML you could use the XML Buffer. If not valid, you need to make < proper then you could try performing some string replace actions on the response output.0
-
I have created a function to try and find the value..
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.0 -
I think the problem is due to the xmlns attribute but I'm not 100% sure. There are some topics about this on Mibuso and I also saw this url. Maybe it helps you further:
https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/350713/how-to-parse-xml-with-xpath-and-namespace/9370720
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions