smtripathi99
11 years agoOccasional Contributor
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?
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?