在Groovy中使用XmlSlurper解析xml [英] Parse xml using XmlSlurper in Groovy

查看:1869
本文介绍了在Groovy中使用XmlSlurper解析xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?xml version =1.0encoding =UTF -8\" >?; 
< soapenv:Envelope xmlns:soapenv =http://schemas.xmlsoap.org/soap/envelope/xmlns:xsd =http://www.w3.org/2001/XMLSchemaxmlns:的xsi = http://www.w3.org/2001/XMLSchema-instance >
< soapenv:Body>
< multiRef xmlns:ns9 =http://hero.ar.vixo.inxmlns:soapenc =http://schemas.xmlsoap.org/soap/encoding/id =id2soapenc :root =0soapenv:encodingStyle =http://schemas.xmlsoap.org/soap/encoding/xsi:type =ns9:IdentityModel>
< vixId xsi:type =xsd:int> 13364719< / vixId>
< / multiRef>
< / soapenv:Body>
< / soapenv:Envelope>

此响应存储在 String 名称中

  def xml = new XmlSlurper()。parseText(xmlMsg); 
def vixId = xml.Body.multiRef.vixId.text()

但是问题在阅读 vixId 之前,我想验证 multiRef 标记中的'type'是否是 IdentityModel



我尝试访问类型,如下所示,但徒劳无功: - p>

  def vixId = xml.Body.multiRef。@ type.text()

请注意,我可以在 multiRef id

$ p $ def vixId = xml.Body.multiRef。@ id.text()

请帮助我访问 type in multiRef tag

编辑:
请注意我想解析类型 code>> multiRef 标记而不使用名称空间,如 multiRef。'@ xsi:type'因为我的 namespace 可能会改变。我想要的是 multiRef 标签有一个属性类型,它的值为 IdentityModel ..只有这样,那么我想读 vixId
还要注意,在groovy 1.8中,我使用 multiRef。@ type 解析了它,但没有使用名称空间,但是自从我更新groovy到2.4.7后, p>

PS: - 我在处理xmls方面很新颖

解决方案

默认的XMLSlurper不支持名称空间。这可以通过使用declareNamespace方法声明名称空间来打开。

  def xml = new XmlSlurper()。parseText(xmlMsg)
.declareNamespace('xsi':'http://www.w3.org/2001/XMLSchema-instance')
def vixId = xml.Body.multiRef.vixId.text()
println vixId

def type = xml.Body.multiRef。@'xsi:type'.text()
println type

输出结果如下:

pre $ $ $ $ $ $ $
ns9:IdentityModel

返回字符串值 ns9:IdentityModel 这是XML中的确切值。如果想剥离命名空间前缀,可以执行类似于 type = type.replace('ns9:','')的事情,最后以IdentityModel结束。


I have an xml response which looks like :-

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <multiRef xmlns:ns9="http://hero.ar.vixo.in" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns9:IdentityModel">
         <vixId xsi:type="xsd:int">13364719</vixId>
      </multiRef>
   </soapenv:Body>
</soapenv:Envelope>

This response is stored in a String name xmlMsg I am trying to parse it as follows:-

def xml = new XmlSlurper().parseText(xmlMsg);
def vixId = xml.Body.multiRef.vixId.text()

But the problem here is that before reading vixId i want to verify if 'type' in multiRef tag is IdentityModel

I tried accessing type as follows, but in vain :-

def vixId = xml.Body.multiRef.@type.text()

Please note that i am able to access id in multiRef tag using as follows:-

 def vixId = xml.Body.multiRef.@id.text()

Please help me in accessing type in multiRef tag

EDIT: Please note that i want to parse the type in multiRef tag without using name space like multiRef.'@xsi:type' because my namespace could change. All i want is that multiRef tag has a attribute type and that has a value of IdentityModel.. Only if this is there then I want to read vixId. Also note that with groovy 1.8 i was parsing it without namespace using multiRef.@type but it has stopped working ever since i updated groovy to 2.4.7

PS:- I am fairly new in dealing with xmls

解决方案

By default XMLSlurper is not namespace aware. This can be turned on by declaring namespaces with the declareNamespace Method.

def xml = new XmlSlurper().parseText(xmlMsg)
             .declareNamespace('xsi' : 'http://www.w3.org/2001/XMLSchema-instance')
def vixId = xml.Body.multiRef.vixId.text()
println vixId

def type = xml.Body.multiRef.@'xsi:type'.text()
println type

The output is:

13364719
ns9:IdentityModel

This returns the string value ns9:IdentityModel which is the exact value in the XML. If want to strip the namespace prefix, can do something like type = type.replace('ns9:','') to end up with "IdentityModel".

这篇关于在Groovy中使用XmlSlurper解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆