简体   繁体   中英

Parsing XML response of SOAP web service with RPG and HTTPLIB

I want to parse this xml response with ILE RPG (Fully-Free RPG) in a data structure with a field for currency and a field for the value.

Thats my response from soap webservice:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <GetCurrentExchangeRatesResult>&lt;MNBCurrentExchangeRates&gt;&lt;Day date="2021-12-09"&gt;&lt;Rate unit="1" curr="AUD"&gt;231,49&lt;/Rate&gt;&lt;Rate unit="1" curr="BGN"&gt;187,05&lt;/Rate&gt;&lt;Rate unit="1" curr="BRL"&gt;58,41&lt;/Rate&gt;&lt;Rate unit="1" curr="CAD"&gt;254,93&lt;/Rate&gt;&lt;Rate unit="1" curr="CHF"&gt;350,64&lt;/Rate&gt;&lt;Rate unit="1" curr="CNY"&gt;50,92&lt;/Rate&gt;&lt;Rate unit="1" curr="CZK"&gt;14,38&lt;/Rate&gt;&lt;Rate unit="1" curr="DKK"&gt;49,20&lt;/Rate&gt;&lt;Rate unit="1" curr="EUR"&gt;365,85&lt;/Rate&gt;&lt;Rate unit="1" curr="GBP"&gt;426,84&lt;/Rate&gt;&lt;Rate unit="1" curr="HKD"&gt;41,45&lt;/Rate&gt;&lt;Rate unit="1" curr="HRK"&gt;48,61&lt;/Rate&gt;&lt;Rate unit="100" curr="IDR"&gt;2,25&lt;/Rate&gt;&lt;Rate unit="1" curr="ILS"&gt;104,13&lt;/Rate&gt;&lt;Rate unit="1" curr="INR"&gt;4,28&lt;/Rate&gt;&lt;Rate unit="1" curr="ISK"&gt;2,48&lt;/Rate&gt;&lt;Rate unit="100" curr="JPY"&gt;284,60&lt;/Rate&gt;&lt;Rate unit="100" curr="KRW"&gt;27,50&lt;/Rate&gt;&lt;Rate unit="1" curr="MXN"&gt;15,41&lt;/Rate&gt;&lt;Rate unit="1" curr="MYR"&gt;76,66&lt;/Rate&gt;&lt;Rate unit="1" curr="NOK"&gt;36,17&lt;/Rate&gt;&lt;Rate unit="1" curr="NZD"&gt;219,85&lt;/Rate&gt;&lt;Rate unit="1" curr="PHP"&gt;6,41&lt;/Rate&gt;&lt;Rate unit="1" curr="PLN"&gt;79,17&lt;/Rate&gt;&lt;Rate unit="1" curr="RON"&gt;73,91&lt;/Rate&gt;&lt;Rate unit="1" curr="RSD"&gt;3,11&lt;/Rate&gt;&lt;Rate unit="1" curr="RUB"&gt;4,39&lt;/Rate&gt;&lt;Rate unit="1" curr="SEK"&gt;35,70&lt;/Rate&gt;&lt;Rate unit="1" curr="SGD"&gt;236,93&lt;/Rate&gt;&lt;Rate unit="1" curr="THB"&gt;9,66&lt;/Rate&gt;&lt;Rate unit="1" curr="TRY"&gt;23,50&lt;/Rate&gt;&lt;Rate unit="1" curr="UAH"&gt;11,93&lt;/Rate&gt;&lt;Rate unit="1" curr="USD"&gt;323,22&lt;/Rate&gt;&lt;Rate unit="1" curr="ZAR"&gt;20,47&lt;/Rate&gt;&lt;/Day&gt;&lt;/MNBCurrentExchangeRates&gt;</GetCurrentExchangeRatesResult>
        </GetCurrentExchangeRatesResponse>
    </s:Body>
</s:Envelope>

The currencies are separated just with &lt .
I tried it like this, but my variable xmlout is empty after the xml-into.

dcl-ds xmlout qualified;
  Tempout char(2129);
END-DS;   

xml-into xmlout %xml(postResult: 'case=any ns=remove allowextra=yes +
    path=Envelope/Body/GetCurrentExchangeRatesResponse/GetCurrentExchangeRatesResult');      

So how can I change or extend my to code to get all the currencies and their values into a structured ds?

For the first XML-INTO, code xmlout.TempOut instead of just xmlout. In the XML document, GetCurrentExchangeRatesResult doesn't match a data structure.

Then you'd need a second XML-INTO to parse the new XML document in xmlout.TempOut.

Something like this, although varchar is probably not the right data type for everything.

dcl-ds MNBCurrentExchangeRates qualified;
   dcl-ds day;                           
      date date(*iso);                   
      num_rate int(10);                  
      dcl-ds rate dim(500);              
         unit varchar(10);               
         curr varchar(10);               
         value varchar(20);              
      end-ds;                            
   end-ds;                               
end-ds; 
                       
xml-into MNBCurrentExchangeRates 
         %xml(xmlout.Tempout  
            : 'case=any countprefix=num_ datasubf=value');  

Tip: When developing a new XML-INTO, avoid using the allowextra or allowmissing options at first. Use a simplified version of the document without any extra or missing XML items until your data structure matches the XML document in general. If you code allowextra=yes or allowmissing=yes too soon, XML-INTO will accept almost anything without giving any error messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM