Forum Discussion

smtripathi99's avatar
smtripathi99
Occasional Contributor
10 years ago

Response XML Parsing with groovy

i have a test which gets the response and parse it to get some value and store in properties. However my groovy gives me the attached error while parsing. Below is the code:

import com.eviware.soapui.support.xml.XmlUtils;

def response= context.testCase.testSteps["GetCabinets Request"].getPropertyValue("response").toString()

log.info response
def node = new XmlParser().parseText(response)
def x = node.e.size()
int i
for ( i=0; i< x; i++)
{

if (node.e.Attributes.object_name.e.text() == "Product Labeling")
{
log.info ("Object Name -- " + node.e.Attributes.object_name.e.text())
log.info ("Object ID --" + node.e.ObjectId.text())
break
}

}




here is the response:

<Response>
<e>
<Attributes>
<object_name>
<e>Components</e>
</object_name>
</Attributes>
<ObjectId>0c002758800015ce</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>Content</e>
</object_name>
</Attributes>
<ObjectId>0c002758800015cf</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>MYTESTCABINET</e>
</object_name>
</Attributes>
<ObjectId>0c0027588000f629</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>Product Labeling</e>
</object_name>
</Attributes>
<ObjectId>0c002758800015d1</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>Resources</e>
</object_name>
</Attributes>
<ObjectId>0c00275880000130</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>System</e>
</object_name>
</Attributes>
<ObjectId>0c00275880000106</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>Temp</e>
</object_name>
</Attributes>
<ObjectId>0c00275880000107</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>Templates</e>
</object_name>
</Attributes>
<ObjectId>0c0027588000012f</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>dctm70</e>
</object_name>
</Attributes>
<ObjectId>0c00275880000105</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>x70</e>
</object_name>
</Attributes>
<ObjectId>0c00275880000104</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>xmContent</e>
</object_name>
</Attributes>
<ObjectId>0c00275880001c2f</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
<e>
<Attributes>
<object_name>
<e>xmLabeling</e>
</object_name>
</Attributes>
<ObjectId>0c002758800015d0</ObjectId>
<ObjectType>dm_cabinet</ObjectType>
<Type>Cabinet</Type>
</e>
</Response>

But when i click Format XML in the XML response tab of SoapUI and again run the script no error comes?

4 Replies

  • smtripathi99's avatar
    smtripathi99
    Occasional Contributor
    By default the response is in JASON:

    [
    {
    "ObjectId": "0c002758800015ce",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Components"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c002758800015cf",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Content"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c0027588000f629",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["MYTESTCABINET"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c002758800015d1",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Product Labeling"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880000130",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Resources"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880000106",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["System"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880000107",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Temp"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c0027588000012f",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["Templates"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880000105",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["dctm70"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880000104",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["x70"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c00275880001c2f",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["xmContent"]},
    "Type": "Cabinet"
    },
    {
    "ObjectId": "0c002758800015d0",
    "ObjectType": "dm_cabinet",
    "Attributes": {"object_name": ["xmLabeling"]},
    "Type": "Cabinet"
    }
    ]
  • akbbhatt's avatar
    akbbhatt
    New Contributor
    There are other ways to do the same thing using Slurper classes instead of XMLUtils.
    Since your response is Json you can use JsonSlurper for this:

    In your test step assertion, add this code:
    import groovy.json.*
    def response = new JsonSlurper().parseText(messageExchange.response.responseContent)
    log.info response

    Now you will see the response in the log.

    You can google for JsonSlurper examples, let me know if you need any more info on the same.
  • aly's avatar
    aly
    New Contributor

    Hello, guys!

     

    I just have similiar problem. I have test case with Run test case element in it. And in this run test case element I need somehow to get response of one of the request in the source test case. But I am using free version of Soap UI. Is it possible to do it in it?

    Thank you