Unable to parse xml attributes without using namespace while parsing using Groovy

131 views Asked by At

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 = [email protected]()

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

 def vixId = [email protected]()

Please help me in accessing type in multiRef tag

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

The way i was parsing it with groovy 1.8 is :

def xml = new XmlSlurper(factory.newSAXParser().getXMLReader()).parseText(xmlMsg)
idModel = [email protected]()

PS:- I am fairly new in dealing with xmls

0

There are 0 answers