繁体   English   中英

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

[英]how I extract field from xml string in java / 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库可以简化它: 标准的Scala XML库

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM