Forum Discussion
ChrisAdams
Champion Level 1
4 years agoHi,
I don't have a service that returns that XML, so I used a Groovy script step to return the XML as a string.
Let's call it "Return XML as a String - Groovy Script". Below is the contents of that step.
def originalXml = '''
<response version-api="2.0">
<Service>
<Values>
<Value Row="31" Column="A"/>
<Value Row="31" Column="C"/>
<Value Row="31" Column="D"/>
<Value Row="31" Column="L"/>
<Value Row="32" Column="D"/>
<Value Row="32" Column="F"/>
<Value Row="33" Column="F"/>
<Value Row="43" Column="J"/>
</Values>
</Service>
</response>
''';
return originalXml;
Note : I had to add the response node within.
To parse, I created another Groovy script step too parse and populate a map of Row and Column values.
Here are the contents of that step...
// We need this to parse our XML String.
import groovy.xml.XmlSlurper;
// Simple class to add to a map later...
class Coordinate {
String row
String column
}
// Let's get our XML String from another Groovy step. This should be replaced by a getting XML from the service response.
def xmlString = context.expand( '${Return XML as String - Groovy Script#result}' )
// Let's have a look at it.
log.info(xmlString);
// Use XML Slurper to parse our XML String.
def response = new XmlSlurper().parseText(xmlString);
// Create an empty map to put the coordinates into.
def map = [];
// Iterate over the values and for each, create a coordinate object and add it into our map.
for (item in response.Service.Values.Value) {
def rowVal = item.attributes()["Row"];
def colVal = item.attributes()["Column"];
// Let's have a look at the current Row and Column
log.info("Row ${rowVal}. Column ${colVal}.");
// Create a temp object.
Coordinate temp = new Coordinate(row: rowVal, column: colVal);
// Add the temp object to our map.
map.add(temp);
}
// Let's have a look at each object in the map....
log.info("");
log.info("Map Contents");
log.info("");
for(item in map) {
log.info("Row ${item.row}. Column ${item.column}.");
}
// Finally, return our map for use elsewhere...
return map;