0
votes

Trouble using XmlParser in SoapUI Pro

Hi, I am trying to use 'xml parser' to validate my xml responses in SoapUI Pro.

I have been playing around with this in a groovy script and I can access the tags if I declare and assign my xml within the groovy script like so

This works if I declare the xml in the script ..

def xml = """
<NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1">
         <NS1:integrationHeader>
            <dateTime xmlns="http://www.royalmailgroup.com/integration  /core/V1">2013-12-24T22:20:34</dateTime>
            <version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version>
            <identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
               <applicationId>111111113</applicationId>
               <transactionId>420642961</transactionId>
            </identification>
         </NS1:integrationHeader>
         <NS1:completedShipmentInfo>
            //xml not complete, other info in here.
         </NS1:completedShipmentInfo>
         <NS1:integrationFooter>
            <warnings xmlns="http://www.royalmailgroup.com/integration/core/V1">
               <warning>
                  <warningCode>W0022</warningCode>
                  <warningDescription>The customerReference specified is longer than   12 characters and has been truncated</warningDescription>
               </warning>
               <warning>
                  <warningCode>W0026</warningCode>
                  <warningDescription>The departmentReference specified is invalid and     will be ignored</warningDescription>
               </warning>
            </warnings>
         </NS1:integrationFooter>
      </NS1:createShipmentResponse>
"""

def parser = new XmlParser().parseText(xml)

parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{ 
     log.info it.text()
}

But it doesn't seem to work within a running test instance when I instantiate the xmlParser variable from my Soap response as below.

def response        = context.expand( '${createShipment_v04#Response}' );

I know that the parser variable has been assigned the xml response because when I can print it to the log ..

i.e. log.info parser prints ...

Wed Jan 08 16:33:38 GMT 2014:INFO:{http://schemas.xmlsoap.org/soap/envelope    /}Envelope[attributes={}; value=[{http://schemas.xmlsoap.org/soap/envelope/}Body[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}createShipmentResponse[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}integrationHeader[attributes={}; .......

But below code does not print anything when I instantiate the xmlParser request from the soap response.

parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{ 
         log.info it.text()
}

Any help would be much appreciated.

3

3 Answers

0
votes

I believe you are working at the wrong level.

parser.Body….

0
votes

Ok. It turns out I don't need the 'NS1:' part. The below works ..

slurper.Body.createShipmentResponse.integrationFooter.warnings.warning.warningCode.each{ 
     log.info it.text()
}
0
votes

Following should work:

def response = context.expand( '${createShipment_v04#Response}' );
def parser = new XmlSlurper().parseText(response)


def warningCodes = parser.'**'.findAll {
    it.name()=='warningCode'
}

warningCodes.each {
  log.info it
}