free seo tool

Parse and Read XML file using Groovy Script

While testing web services, we might end up parsing and reading XML files for different purposes. Let us see how can we use groovy script effectively to parse/ read XML file
In this example, let us consider the below XML file named "DATASHEET.xml".
Now we need a groovy script to read the data from the above XML file. Let us see the below Groovy Script.

def alert = com.eviware.soapui.support.UISupport;
//Define a file pointer for groovy to handle the file operations.
def inputFile = new File("C:\\SOAPUI\\DATASHEET.xml")
if(!inputFile.exists())
{
    //Display an alert if the file is not found.
    alert.showInfoMessage("Input File 'DATASHEET.xml' not found!");   
}
else
{
    //Read and parse XML file and store it into a variable
    def InputXML = new XmlParser().parseText(inputFile.text)
    //Find/ Filter XML nodes based on a condition
    def InputRow = InputXML.Employee.findAll{
        it.Age.text().toInteger() > 19;   
        //We are finding all XML nodes where the age > 19
    }
    InputRow.each{
            //Display the value of name node from the filtered record
            log.info(it.Name.text());
        }
} 

If you want to do data driven testing using groovy script in soapUI to test web services, then you might consider the above script to do the same. Se if you have an XML file with test case name, parameters and an action column to control the execution flow, you can read the XML file by using this groovy script and set the parameters as properties of a test case and use it dynamically. This can be achieved by using the script in conjunction with the soapUI setup and teardown features.

8 comments:
  • Unknown says:
    August 08, 2012

    Hi,
    I am using the above script for a similar scenario of mine in SOAP UI

    def alert = com.eviware.soapui.support.UISupport;

    //Define a file pointer for groovy to handle the file operations.
    def inputFile = new File("C:\\Documents and Settings\\mcas_user\\Desktop\\Gen5\\DataSink.xml")
    if(!inputFile.exists())
    {
    //Display an alert if the file is not found.
    alert.showInfoMessage("Input File 'DATASHEET.xml' not found!");
    }
    else
    {
    //Read and parse XML file and store it into a variable
    def InputXML = new XmlParser().parseText(inputFile.text)
    //Find/ Filter XML nodes based on a condition
    def InputRow = InputXML.resource.findAll{
    it.ext_comp_id.text();
    //We are finding all XML nodes where the age > 19
    }
    InputRow.each{
    //Display the value of name node from the filtered record
    log.info(it.Name.text());
    }
    }

    But i am getting an error saying "org.xml.sax.SAXParseException; lineNumber:1;columnNumber:1;Content is not allowed in prolog."
    at this line of code "def InputXML = new XmlParser().parseText(inputFile.text)".
    Can you please help..its very urgent ?

  • Admin says:
    August 09, 2012

    This could be because the XML file is not formatted in a correct way.

  • SimpleThinking says:
    March 13, 2014

    Hi,
    My requirement is something different, i want the specific node value from the extranal xml. Is there any method from which we can read external XML and get the specific node value?

  • Srinath says:
    September 10, 2015

    Title : Reading data from Excel and converting them to XML

    I have excel sheet with below values
    India
    Telangana
    AP
    Karnataka
    Australia
    Christmas Island
    New South Wales
    QueenIsland

    I want output in xml commands with below format
    < India>









    Below is my code where I am getting output as
    Output : <><><><><><><><>
    Code:
    import java.io.File
    import java.util.Date
    import jxl.*
    import groovy.xml.MarkupBuilder
    import org.custommonkey.xmlunit.*

    wb = Workbook.getWorkbook(new File('F:\\SOAPUI Practice\\Practice\\New Practice\\GetWeather1.xls'))
    sheet = wb.getSheet(0)
    def sw = new StringWriter()
    def xml = ""
    for (int i=0; i" + ""
    }
    }
    log.info xml
    wb.close()


    Can some one help me.

  • Srinath says:
    September 10, 2015

    Sorry there are some inputs missing from the above post

    I want output in xml commands with below format












    With above program i getting output as below(Few Empty tags & all in one line) :

    <><><><><><><><>

  • Anonymous says:
    February 09, 2017

    can anybody help me in transfer property of groovy script

  • Unknown says:
    February 07, 2018

    Hi,

    I have a 36 field request.xml, i need to generate response using that request.
    Storing the input data in excel sheet, Here i want to read the input data from excel file to request

    please do needfull

  • Unknown says:
    October 15, 2018

    Nice blog.it was really informative..Thanks for sharing.i refer this blog to my friends.keep update software testing training with job guarantee
    selenium training in chennai

Post a Comment

This is a new website mainly for SOA Testers using soapUI. Let me know your thoughts/ suggestions.