从这个给定的XML中,我想提取标签的值并将其分配给变量。我正在使用Scala。
<soap:Envelope xmlns:wsdlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:iav="http://www.example.com/xxx/yy/ABC_DE_Fghijklmn">
<soap:Body>
<iav:OperationResponse>
<iav:XYZ>AAA</iav:XYZ>
<iav:Code/>
</iav:OperationResponse>
</soap:Body>
</soap:Envelope>
将给定的XML分配给val resp。 我尝试过以下选项:
(resp.get \\"iav:XYZ")
(resp.get \"@iav:XYZ")
(resp.get \\"@iav:XYZ")
(resp.get \"@iav:XYZ")
但是他们都不返回任何东西。
我将如何实现?我将收到许多这样的响应集,并且必须对所有集合进行相同的提取和分配。
答案 0 :(得分:1)
以下是提取方法:
import scala.xml.Elem
object StackOverflow {
def main(args: Array[String]): Unit = {
val xml = """<soap:Envelope xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:wsdlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:iav="http://www.informatica.com/xxx/yy/ABC_DE_Fghijklmn">
<soap:Body>
<iav:OperationResponse>
<iav:XYZ>AAA</iav:XYZ>
<iav:Code/>
</iav:OperationResponse>
</soap:Body>
</soap:Envelope>"""
val xmlElem: Elem = scala.xml.XML.loadString(xml)
val operationResponse = xmlElem \\ "OperationResponse"
val xyz = operationResponse \ "XYZ"
print( "Operation response is " + xyz.text)
}
}