Forum Discussion

evoks's avatar
evoks
Occasional Contributor
7 years ago
Solved

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.

 

 

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Would you mind to explain what exactly you are trying to achieve? or why do you want to do so? Reason being, if we know the object, there could be alternate better solutions.
    • evoks's avatar
      evoks
      Occasional Contributor
      Hi,
      Thanks for your answer.
      For each index, I want the tag tree to each node, from the "Body" node
      • evoks's avatar
        evoks
        Occasional Contributor

        (sorry for my english, I'm studying it at school).

         

        To be more precise, if I have the following XML file :

         

        <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>

         

        The goal is to capture all parent nodes to each value returned by the groovy script.

         

        For the "value_B1", I have to capture its parent nodes and concatenate them :

        "Body/parent_Node_B/children_NodeB1=value_B1"

         

        Second example, for the "value_D" :

        "Body/parent_Node_D/children_Node_D=value_D"

         

        Third example, for the "value_E" :

        "Body/parent_Node_E=value_E"

         

        I hope I've been more explicit.

         

        Thanks.