Forum Discussion

vijaydi's avatar
vijaydi
Contributor
10 years ago

removeDomNodes with for Loop

Hi,

Below is the portion of the XML request that I am manuplating using groovy script.

<Accounts>
<account recordId="1">
<account recordId="2">
<account recordId="3">
<account recordId="4">
<account recordId="5">
<Accounts>


Based on a value ranging from 1-5 (which I read from a file), I will have to remove the Domnodes. For eg. If the value is 3, then I will have to remove nodes 4 and 5.

I have the below groovy script but what I observe is that it does not remove all the nodes as the position of the nodes change when the loop is exeuted for the first loop value.

def ReqHolder2 = grUtils.getXmlHolder("Modified#Request")

def count = ReqHolder2.getDomNodes('//ARCAAccountReportBatch[1]/Accounts[1]/Account').size()

log.info(count)

noAccounts = Integer.parseInt (context.expand( '${DataSource#noAccounts}' )) ///numeric value I read from the file

for (i in noAccounts+1..count)
{
ReqHolder2.removeDomNodes("//ARCAAccountReportBatch[1]/Accounts[1]/Account[$i]")
}

ReqHolder2.updateProperty()





On exeuting this (with the noAccounts = 2), I get the below updated XML request

<Accounts>
<account recordId="1">
<account recordId="2">
<account recordId="3">

<account recordId="5">
<Accounts>


I would apprecite if anyone could help me with the solution.

Thank you.

2 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Xml snippet you post isn't well formed.
    Would you mind posting complete response? Is it true that always you read single value from file?
  • Thank you for you response nmrao.

    soapUI structure:

    Datasource
    Original
    GroovyScript
    Modified

    Original Request

    attached is the original request.xml

    Groovy Script:

    def grUtils = new com.eviware.soapui.support.GroovyUtils(context)

    noAccounts = Integer.parseInt (context.expand( '${DataSource#noAccounts}' ))
    def fileNo = context.expand( '${DataSource#FileNumber}' )

    //Copy XML content form one request to another (IDM_Original to IDM_Modified)

    def ts = testRunner.testCase.getTestStepByName("Original")
    def ts2 = testRunner.testCase.getTestStepByName("Modified")

    String content = ts.getHttpRequest().getRequestContent()
    ts2.getHttpRequest().setRequestContent(content)


    //Removing Account Nodes

    def ReqHolder2 = grUtils.getXmlHolder("Modified#Request")

    def count = ReqHolder2.getDomNodes('//ARCAAccountReportBatch[1]/Accounts[1]/Account').size()


    log.info(count)

    if (noAccounts <= 4)
    {
    log.info("intheloop")


    for (i in noAccounts+1..5)

    {
    log.info("//ARCAAccountReportBatch[1]/Accounts[1]/Account[$i]")
    log.info("inloop"+i)
    ReqHolder2.removeDomNodes("//ARCAAccountReportBatch[1]/Accounts[1]/Account[$i]")

    }

    }

    else

    {
    log.info("is-11")
    }

    ReqHolder2.updateProperty()

    //write the request and response to a file

    def inputFileRequest = new File("V:\\AccountCreation_XMLs\\"+ "Account_" + fileNo+".xml")
    def request = grUtils.getXmlHolder(context.expand( '${Modified#Request}')).getPrettyXml()
    inputFileRequest.write(request)

    //log.info(grUtils.getXmlHolder(context.expand( '${Modified#Request}')).getPrettyXml())


    Modified Request:

    See attached modified request.xml

    No, the values changes everytime.

    The objective is to create 100+ xml request with different account size. eg..some having only 2, some having 3 etc..

    Thank you.