我如何从java / scala中的xml字符串中提取字段

时间:2022-09-13 11:07:00

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>

1 个解决方案

#1


2  

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

标准的scala xml库可以简化它:标准的Scala XML库

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:

检查scala String到scala.xml.Elem以将字符串转换为scala xml元素。然后,您可以使用scala xml库中的xpath类似的序列来导航和解析xml树。像这样的东西:

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

#1


2  

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

标准的scala xml库可以简化它:标准的Scala XML库

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:

检查scala String到scala.xml.Elem以将字符串转换为scala xml元素。然后,您可以使用scala xml库中的xpath类似的序列来导航和解析xml树。像这样的东西:

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