Forum Discussion

Rebbelx's avatar
Rebbelx
Occasional Contributor
10 years ago

Groovy script get certain tag(s) from response xml

Hi,

 

I am rather new to groovy scripting and have a question. I have a xml request that gives a certain response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soap:Body>
      <ns2:MT_GetValidationRanges_response xmlns:ns2="http://www.eandis.be/CAPTATIE/MF881/SPI893">
         <GetValidationRangesResponse>
            <EANs>
               <EAN>
                  <EANNumber>541448860004900201</EANNumber>
                  <MeteringDevices>
                     <MeteringDevice>
                        <Id>7100627867</Id>
                        <SerialNumber>23598095</SerialNumber>
                        <DeviceLabel>1</DeviceLabel>
                        <Indexes>
                           <Index>
                              <RegisterIdentifier>TH</RegisterIdentifier>
                              <RegisterSequence>1</RegisterSequence>
                              <PrevalidationMin>6674</PrevalidationMin>
                              <PrevalidationMax>7954</PrevalidationMax>
                              <PrevalidationDigits>8</PrevalidationDigits>
                              <PrevalidationDecimals>3</PrevalidationDecimals>
                           </Index>
                        </Indexes>
                     </MeteringDevice>
                  </MeteringDevices>
               </EAN>
            </EANs>
         </GetValidationRangesResponse>
         <Status>
            <Code>0</Code>
            <Description>OK</Description>
         </Status>
      </ns2:MT_GetValidationRanges_response>
   </soap:Body>
</soap:Envelope>

 

 

From this reponse I would like to get the value of <PrevalidationMin> and write this to a csv file.

I already found the following code online

 

def oGroovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def oResponseHolder = oGroovyUtils.getXmlHolder("Single GetValidationRanges" + "#Response")
oResponseHolder.getNodeValue( "PrevalidationMin")

 

But still need some code. I cant find the last part. Can someone help me please ?

 

Thanks

 

 

  • nmrao's avatar
    nmrao
    10 years ago

    Try the below script.

     

    import com.eviware.soapui.support.XmlHolder
    def xml = new XmlHolder(context.response)
    def preValidationMinValue = xml.getNodeValue("//*:PrevalidationMin")
    new File('c:/temp/myfile.csv').write(preValidationMinValue)

    Note that, you may have to change/tune the xpath if you have multiple EAN elements to get the right value.

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Assuming you are happy with capturing the value from the response e.g. using the code you had:

     

    def  prevalidationMinValue = oResponseHolder.getNodeValue( "PrevalidationMin")

     

    And this gives you the value you want, then to write the file you can just do something like: 

     

    new File('/Users/test/yourfile.csv').write(prevalidationMinValue)

     

    Is this what you needed?

     

    Cheers,

    Rupert

    • Rebbelx's avatar
      Rebbelx
      Occasional Contributor

      Rupert,

       

      Thanks for your reply. I have tried your code and it gives me an error:

       

      ERROR:groovy.lang.MissingPropertyException: No such property: prevalidati​onMinValue for class: Script29

    • Rebbelx's avatar
      Rebbelx
      Occasional Contributor

      Rupert,

       

      Thanks for your reply. However I still get an error when trying to create the file. I get a NullPointerException.

       

      When I write

      new File('/yourfile.csv').write("Test") the script creates a new file with the string Test in it.

       

      When I write

      new File('/yourfile.csv').write(PrevalidationMin) I get the NullPointerException

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are you using unix operating system and trying to save on the root directory?
        Any way provide the valid file path(absolute) separated by '/' even on windows platfrom.