[英]Parse xml using XmlSlurper in Groovy
I have an xml response which looks like :-我有一个 xml 响应,它看起来像:-
<?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:-此响应存储在String
名称xmlMsg
我正在尝试按如下方式解析它:-
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
但这里的问题是,在阅读vixId
之前,我想验证multiRef
标签中的“类型”是否为IdentityModel
I tried accessing type
as follows, but in vain :-我尝试按如下方式访问type
,但徒劳无功:-
def vixId = xml.Body.multiRef.@type.text()
Please note that i am able to access id
in multiRef
tag using as follows:-请注意,我可以使用如下方法访问multiRef
标签中的id
:-
def vixId = xml.Body.multiRef.@id.text()
Please help me in accessing type
in multiRef
tag请帮助我访问multiRef
标签中的type
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.编辑:请注意,我想解析multiRef
标记中的type
,而不使用像multiRef.'@xsi:type'
namespace
因为我的namespace
可能会改变。 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
.我想要的只是multiRef
标签有一个属性type
并且它的值为IdentityModel
.. 只有当它存在时,我才想读取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另请注意,使用 groovy 1.8 时,我使用multiRef.@type
在没有命名空间的情况下解析它,但自从我将 groovy 更新为 2.4.7 后它就停止工作了
PS:- I am fairly new in dealing with xmls PS:-我在处理xmls方面还很陌生
By default XMLSlurper is not namespace aware.默认情况下,XMLSlurper 不知道命名空间。 This can be turned on by declaring namespaces with the declareNamespace Method.这可以通过使用 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
The output is:输出是:
13364719
ns9:IdentityModel
This returns the string value ns9:IdentityModel
which is the exact value in the XML.这将返回字符串值ns9:IdentityModel
,它是 XML 中的确切值。 If want to strip the namespace prefix, can do something like type = type.replace('ns9:','')
to end up with "IdentityModel".如果想type = type.replace('ns9:','')
命名空间前缀,可以执行类似type = type.replace('ns9:','')
操作,以“IdentityModel”结尾。
Did not exactly find what i was looking for yet, but i have used a temporary workaround for the time being :-还没有完全找到我要找的东西,但我暂时使用了一个临时的解决方法:-
if (xml.Body.multiRef.attributes().toString().contains("IdentityModel")) {
vixId = xml.Body.multiRef.vixId.text()
}
Here attributes()
method will give an array of all the attributes, their namespaces and values这里的attributes()
方法将给出所有属性的数组,它们的命名空间和值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.