1
votes

with the XML code below:

def soapResponse = "

<Results>
   <ResultSet fetchSize="10">
      <Row rowNumber="1">
         <ID_ORDER>144107</ID_ORDER>
         <CH_LNAME>TESTING</CH_LNAME>
         <CH_FNAME>QA</CH_FNAME>
      </Row>
      <Row rowNumber="2">
         <ID_ORDER>144108</ID_ORDER>
         <CH_LNAME>TESTING</CH_LNAME>
         <CH_FNAME>QA</CH_FNAME>
      </Row>
   </ResultSet>
</Results>

"

def inputXML = new XmlParser().parseText(soapResponse)

I want to use a GPath to get the value of ID_ORDER under the first Row (Row rowNumber="1").

This works, the below variable is populated with 144107:

def id_order = inputXML.ResultSet.Row[0].ID_ORDER

However, I need to be able to construct that GPath from an excel sheet. But when I populate the Excel cell with ResultSet/Row[0]/ID_ORDER and perform the above line as a variable:

def excelVariable = //getting "ResultSet/Row[0]/ID_ORDER" from sheet
def excelVariableArray = excelVariable.split('/')  //create array of it, separated by "/"
def id_order = inputXML."${excelVariableArray[0]}"."${excelVariableArray[1]}"."${excelVariableArray[2]}".text()  //traverse through XML, get value of "ID_ORDER" under 1st "Row"element

Here id_order is not populated. The expectation is that it would populate with 144107. If I take the index "[0]" out of the Row[0], then it successfully grabs all of the ID_ORDER values from the XML. But since I only want the 1st one, I use the index 0 but it doesn't work - value comes out empty.

Hoping someone can chime in with a resolution to this.

1
You need to be very careful, a specially crafted excel sheet could exploit your system. Where do these sheets come from? - tim_yates
It is an Excel sheet with test data in it, located in the project's folder of a Ready! API project. - user1599401

1 Answers

1
votes

One way to do it is to execute the excelVariable expression in a new GroovyShell, with appropriate bindings:

def inputXMLStr = """
<Results>
   <ResultSet fetchSize="10">
      <Row rowNumber="1">
         <ID_ORDER>144107</ID_ORDER>
         <CH_LNAME>TESTING</CH_LNAME>
         <CH_FNAME>QA</CH_FNAME>
      </Row>
      <Row rowNumber="2">
         <ID_ORDER>144108</ID_ORDER>
         <CH_LNAME>TESTING</CH_LNAME>
         <CH_FNAME>QA</CH_FNAME>
      </Row>
   </ResultSet>
</Results>
"""

def inputXML = new XmlSlurper().parseText(inputXMLStr)

def map = [:]
map["inputXML"] = inputXML
def binding = new Binding(map)
def shell = new GroovyShell(binding)

def excelVariable = "inputXML.ResultSet.Row[0].ID_ORDER" 
def result = shell.evaluate(excelVariable)
assert result == inputXML.ResultSet.Row[0].ID_ORDER