Forum Discussion

vikram_9669's avatar
vikram_9669
Occasional Contributor
10 years ago

How to update tag value in external xml using groovy script?

I am trying to update values of node <tem:intA> and <tem:intB> in the following xml using below groovy script code.

 

UpdateXmlTagValue.jpg

 

I used below groovy script code to update the tag values. But I am getting an error "Java.lang.NullPointerException: Cannot invoke method getNodeValue() on null object error at line 36".

 

UpdateXmlTagValue2.jpg

 

Am I using the wrong xpath? I am not sure whats going wrong. Can someone please help me?

 

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Ok, so if I may suggest the use of XMLSlurper, which is built into Groovy so requires no imports and is quote neat. Then if we put your XML snippet (similar typed in by me) into a file /work/SoapUI Forum/soapy.xml:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
    <soap:Header/>
    <soap:Body>
    <tem:Add xmlns:tem="http://tempuri.org">
    <tem:intA>5</tem:intA>
    <tem:intB>7</tem:intB>
    </tem:Add>
    </soap:Body>
    </soap:Envelope>

     

    Then, the following Groovy script allows you to parse the XML from file, update the values of intA and intB and save the resulting XML back to the same file:

     

    import groovy.xml.XmlUtil

    def xmlFromFile = new File('/work/SoapUI Forum/soapy.xml')
    def envelopeNode = new XmlSlurper().parseText(xmlFromFile.getText())

     

    envelopeNode.Body.Add.intA=5
    envelopeNode.Body.Add.intB=7

     

    //Just to print it out
    XmlUtil xmlUtil = new XmlUtil()
    log.info xmlUtil.serialize(envelopeNode)

     

    //To update the file with updated XML
    xmlUtil.serialize(envelopeNode, new FileWriter(xmlFromFile))

     

    Is this what you needed?

     

    Cheers,

    Rup

    • vikram_9669's avatar
      vikram_9669
      Occasional Contributor

      Thanks Rup!!

       

      This is what I was looking for. But how to parameterize both xpath and and node value. Lets say, I am taking this xpath (envelopeNode.Body.Add.intA) as a input parameter from excel sheet and then modifying the value to '20'.

       

      def sIntA=envelopeNode.Body.Add.intA     // Lets say, this value is taken from excel sheet (I have a code to get the value from excel sheet)

       

      My aim is to get the Node's xpath from excel sheet, get the Node's value from excel sheet, update the xml with this node and save updated xml in a different folder.

       

      Please suggest.

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        Thats ok.

         

        I think I now get what you are after i.e. a way to pass the actual (XPath) query in as a String paramter, but before having a go at that, can I ask if you actually just want a way to parameterise and populate SOAP requests from your spreadsheet of values e.g. data-driven testing? If this is the case I can potentially explain an easier way to do this in SoapUI using your spreadsheet.

         

        Cheers,
        Rup