evoks
8 years agoOccasional Contributor
SOAPUI - Groovy : Get parent names / children names / nodes values from XML response
Hi,
I am parsing an xml response with SOAPUI and using a Groovy script like this.
The goal of this script is to get children nodes names and values in xml response.
def parsing = """ <Body> <parent_Node_A>value_A</parent_Node_A> <parent_Node_B> <children_Node_B1>value_B1</children_Node_B1>
<children_Node_B2>value_B2</children_Node_B2>
</parent_Node_B> <parent_Node_C>value_C</parent_Node_C> <parent_Node_D> <children_Node_D>value_D</children_Node_D> </parent_Node_D> <parent_Node_E>value_E</parent_Node_E> </Body> """ def parsingResult = new XmlSlurper().parseText(parsing) def ArrayList_parsing = [] def resultsParsing resultsParsing = {output,node -> node.children().each{resultsParsing(ArrayList_parsing,it) } if (node.children().size()==0) output << node.name()+'='+node.text() } parsingResult.'**'.findAll { it.name() == 'Body' }.each { resultsParsing(ArrayList_parsing,it) } for (int i=0;i<ArrayList_parsing.size();i++) { log.info ArrayList_parsing[i] }
This is almost what I want !
The output of the code above is :
parent_Node_A=value_A
children_Node_B1=value_B1
children_Node_B2=value_B2
parent_Node_C=value_C
children_Node_D=value_D
parent_Node_E=value_E
I have now to modify my code to have the parent name of each children but I don't get it :(
Body/parent_Node_A=value_A Body/parent_Node_B/children_Node_B1=value_B1
Body/parent_Node_B/children_Node_B2=value_B2 Body/parent_Node_C=value_C Body/parent_Node_D/children_Node_D=value_D Body/parent_Node_E=value_E
If anyone of you could help me ?
Many thanks in advance.
BR,
Anthony.
This is similar to https://stackoverflow.com/questions/36179764/get-path-to-all-xmls-nodes
private static String getXPath(node) { if (node.parent().name() == node.name()) { return node.name() } return getXPath(node.parent()) + "/" + node.name() }
then use
output << getXPath(node)+'='+node.text()