Sanjay Rabari
Sanjay Rabari

Reputation: 2081

how I extract field from xml string in java / scala

I have following as a string and I want to get values from it like response code and response message how I can get that

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="https://exa.org/UpdateOrder/">
<soapenv:Body>
<tns:UpdateResponse xmlns="https://exa.org/UpdateOrder/">
<tns:responseCode>0</tns:responseCode>
<tns:responseMessage/>
</tns:UpdateResponse>
</soapenv:Body>
</soapenv:Envelope>

Upvotes: 1

Views: 1546

Answers (1)

Dan M
Dan M

Reputation: 111

The standard scala xml library could make short work of it: The standard Scala XML library

Check out scala String to scala.xml.Elem to convert the string to an scala xml element.
Then you can navigate and parse the xml tree using xpath like sequences in the scala xml library. Something like this:

val xmlNode = scala.xml.XML.loadString(string)
val responseCode = (xmlNode \ "Body" \ "UpdateResponse" \ "responseCode").text

Upvotes: 3

Related Questions