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

标签 java xml scala

我有以下字符串,我想从中获取值,例如响应代码和响应消息,如何获取它

<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/

相关文章:

java - 为什么 SimpleDateFormat 解析不正确的日期?

jquery - 当浏览器请求时,CORS 响应 header 在 Spray 中不起作用

java - 如何从客户端 GWT 应用程序读取 JNDI 属性

regex - 如何使用 Perl 从 XML 中的多个根元素打印结果

Python:如何编辑 XML 文件的元素?

android,xml按钮布局问题

scala - 除了在宏中给我源位置之外,-Yrangepos 的作用是什么

java - Junit5 测试报告程序

java - 实例化通用类数组

java - 创建谓词以将实体与 Map 属性中的条目匹配