Forum Discussion

doooit's avatar
doooit
Visitor
6 years ago

Extract multi values from xml

The xml below is an example result of the rest api request,

1) how can I get parent and children's id and name?

2) how can I use the first children id for the next request step?

3) how to extract two or more request results together in a json format file?

Thanks!

<body>
	<Parent>
		<id>111</id>
		<property>
			<name>111</name>
			<info>...</info>
		</property>
	</parent>
	<children>
		<properties>
			<property>
				<id>112</id>
				<name>112</name>
				<info>...</info>
			</property>
			<property>
				<id>113</id>
				<name>113</name>
				<info>...</info>
			</property>
			<property>
				<id>114</id>
				<name>114</name>
				<info>...</info>
			</property>
		</properties>
	</children>
</body>

 

 

3 Replies

  • Radford's avatar
    Radford
    Super Contributor

    Is the XML the result of a REST test step? If so can you use the point and click Get Data functionality, from where you need the data?

     

    If you need to use Groovy (I would always recommend using out of the box functionality before resorting to Groovy script) then the following is an example of how to use the XML Slurper to extract data, this should answer points 1 & 2, plus give a couple more examples. This is a stand alone example that can be pasted into an empty Groovy Script test step and run:

     

    def xmlText = '''\
    <body>
        <parent>
            <id>111</id>
            <property>
                <name>111</name>
                <info>...</info>
            </property>
        </parent>
        <children>
            <properties>
                <property>
                    <id>112</id>
                    <name>112</name>
                    <info>...</info>
                </property>
                <property>
                    <id>113</id>
                    <name>113</name>
                    <info>...</info>
                </property>
                <property>
                    <id>114</id>
                    <name>114</name>
                    <info>...</info>
                </property>l
            </properties>
        </children>
    </body>'''
    
    import groovy.util.XmlSlurper
    
    def body = new XmlSlurper().parseText(xmlText)
    
    // Log the parent id
    log.info('Parent ID = ' + body.parent.id)
    
    // Log the first child id
    log.info('First (index zero) child ID  = ' + body.children.properties.property[0].id)
    
    // Log all child ids
    body.children.properties.property.each(){ prop ->
    	log.info('Child id = ' + prop.id)
    }
    
    // Log all child ids with index
    body.children.properties.property.eachWithIndex(){ prop, idx ->
    	log.info('ID of child at index ' + idx + ' = ' + prop.id)
    }
    
    // Log the child id whose name is "113"
    def namedProp = body.children.properties.'*'.find {prop -> prop.name=='112'}
    log.info('namedProp ID = ' + namedProp.id)
    

     

     

    Regarding your 3rd question, sorry I'll have to leave that to someone else.

  • richie's avatar
    richie
    Community Hero

    Hi doooit

     

    whoops - sorry - it took me that long to type this out that Radford typed out his answer first - so I don't know if this helps at all (it doesn't if you want the groovy option)

     

    there's multiple options for extracting values from a response - groovy script or the OOB functionality - which resolves points 1 and 2 - but can you explain exactly what you're trying to do and flesh out your point 3 requirement please?  This will then assist in people tailoring the relevant answer.

     

    If you just need to extract the values of certain tags for later use - here's one option - which is using the OOB  property transfer functionality to store in a Properties test step - allowing you to 'store' the values for later use in your test.

     

    Possible Answer to Point 1 (extract Parent Name tag value)

    Here's how I would grab your parent name tag value

    In the 'Outline' tab, highlight the parent name attribute you want (//body/parent/property/name) and click on 'transfer to' button

    Select 'Add Properties step', click on 'Ok' on dialogue to accept name of properties step

    click on 'Ok' on 'Create target property' dialogue (to set the name of the property)

    'Transfer to Property' form launches - just click 'OK' to accept the defaults for now to  create the Property Transfer step.

    I then ensure the sequence of the steps is correct - so that the Property Transfer step is AFTER the step that generated your XML response and the Properties step is after your Property Transfer step (so your step hierarchy in the test is as follows):

     

    Test Suite

         Test Case

             Rest Request

             Property Transfer (1 property transfer, transfers the Parent name value to the Properties step)

             Properties (contains the Parent name tag value)

     

    The above stores your parent's name tag value in the Properties step - for later use.

    At this point, I would then repeat the above (changing the sequence slightly to grab the Parent's id tag value as follows:

     

    Continuation of Possible Answer to Point 1 (extract Parent id tag value)

    In the 'Outline' tab, highlight the parent id tag you want (//body/parent/id) and click on 'transfer to' button

    Select  the 'Properties' step in the menu -

    Click on 'Create new'

    Click on 'Ok' on 'Create target property' dialogue (to set the name of the property)

    'Transfer to Property' form launches - just click 'OK' to accept the defaults for now.

    Double click on the 'Property Transfer' step in your test case, look at the 'Target' section - the 'Property' field is blank (in v2.4.0) - so you need to set this property to the one created a couple of steps ago

     

    At this point - I double check if the property transfer runs correctly for each (both) property transfers - to do this I highlight the first property, then I click the green right arrow (alt text = ''Runs selected Property Transfer') and you can see the value successfully captured in the 'Transfer Log' area of the screen - i do this for each property..

     

    A final check is to run your test to ensure the property transfers work on execution and once the test has run - you should be able to see the 2 properties written to the 'Properties' test step also.

     

    So your hierarchy then looks like this

    Test Suite

         Test Case

             Rest Request

             Property Transfer (2 property transfers, transfers the Parent name, and Parent id value to the Properties step)

             Properties (contains the Parent name and id tag values in 2 properties that were created)

     

    Continuation of Possible Answer to Point 1 (extract Child id tag value)

    ** repeat 'Continuation of Possible Answer to Point 1 (extract Parent id tag value)' steps but start off with the child id tag instead of the Parent id tag

     

    In regards to an answer to Point 2 - could you please provide the content of the request that needs to be populated with the first child's id tag value (from the response of first request)?  It will help in answering your question.

     

    In regards to an answer to Point 3 - if you can provide more info about exactly what you need - that can only help people.

     

    cheers,

     

    richie

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

      Thank you, guys, for your assistance! :cathappy: :cathappy:

       

      doooit, are the first two questions answered? Do you have any additional details for the Community regarding your third question?

      We are looking forward to hearing from you.