Parsing XML with Kotlin?

I have this xml response from fedex webservices that looks like:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">     <SOAP-ENV:Header/>     <SOAP-ENV:Body>         <TrackReply xmlns="http://fedex.com/ws/track/v18">             <HighestSeverity>SUCCESS</HighestSeverity>             <CompletedTrackDetails>                 <Notifications>                     <Severity>SUCCESS</Severity>                     <Source>trck</Source>                     <Code>0</Code>                     <Message>Request was successfully processed.</Message>                     <LocalizedMessage>Request was successfully processed.</LocalizedMessage>                 </Notifications>                 <DuplicateWaybill>false</DuplicateWaybill>                 <MoreData>false</MoreData>                 <TrackDetailsCount>0</TrackDetailsCount>                 <TrackDetails>                     *<StatusDetail>                         <CreationTime>2020-06-24T00:00:00</CreationTime>                         **<Code>DL</Code>**                         <Description>Delivered</Description>                         <Location>                             <City>New York</City>                             <StateOrProvinceCode>New York</StateOrProvinceCode>                             <CountryCode>US</CountryCode>                             <CountryName>United States</CountryName>                             <Residential>false</Residential>                         </Location>                     </StatusDetail>*                     <CarrierCode>FDXE</CarrierCode>                 </TrackDetails>             </CompletedTrackDetails>         </TrackReply>     </SOAP-ENV:Body> </SOAP-ENV:Envelope>  

I want to store and do something with the information in StatusDetail, specifically the information within Code that stores -> DL

Any help will be appreciated.

Asked on July 17, 2020 in XML.
Add Comment
1 Answer(s)

First, download their WSDL:

  1. Login to the FedEx Developer Resource Center.
  2. From the navigation area of the page, click the link ‘FedEx Web Services’.
  3. Click the link ‘Documentation and Downloads’.
  4. Find the functionality you are interested in.
  5. Click on either the WSDL or XSD link where it says ‘Download WSDL or XSD’ next to the download arrow of the appropriate service.

Note: You can download the schema in either WSDL or XML format, depending on which link you choose. Also, every service has its own WSDL guide that contains more detailed information on that specific service then the developer guide. Use both guides as best practice.

Then, get familiar with Apache CXF. You can use CXF’s wsdl2java tool to generate Java code from the WSDL you’ve downloaded. If you’re using Maven, they also have a cxf-codegen-plugin, which is basically the same. Gradle doesn’t have one, but it’s easy to implement a wsdl2java task based on CXF classes.

Anyway, compile the generated Java classes into a library (it really won’t change often) and use them in your Kotlin project!

This way, you don’t really need to parse the XML, as the generated Java client will do everything for you and you’ll be working directly with JavaBeans / POJOs.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.