我有以下字符串,我想从中获取值,例如响应代码和响应消息,如何获取它
<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>
最佳答案
标准的 scala xml 库可以轻松完成它:The standard Scala XML library
查看scala String to scala.xml.Elem将字符串转换为 scala xml 元素。
然后,您可以使用 scala xml 库中类似 xpath 的序列来导航和解析 xml 树。像这样的事情:
val xmlNode = scala.xml.XML.loadString(string)
val responseCode = (xmlNode \ "Body" \ "UpdateResponse" \ "responseCode").text
关于java - 如何从 java/scala 中的 xml 字符串中提取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44976259/