cancel
Showing results for 
Search instead for 
Did you mean: 

Need to extract the value of an attribute from response XML

Oct21
Occasional Contributor

Need to extract the value of an attribute from response XML

Hi 

 

I want to extract the value of orderItemRef from both assessment and report tag from response XML

 

<AssessmentOrder>
<Assessment packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="2183404" language="MULTI" stage="1" jobLevel="Graduate/University" originalTimer="00:25:00" adjustedTimer="00:25:00"/>
<Report packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="1711998" language="en-US" comparisonGroup="" reportFormat="Pdf" reportMedia="Link"/>
<AssessmentAccess hasAssessments="true">
<Assessment stage="1">
</Assessment>
</AssessmentAccess>
</AssessmentOrder>

 

Please help.

 

Thanks

Oct21

13 REPLIES 13
avidCoder
Super Contributor

Re: Need to extract the value of an attribute from response XML

Try the below code and consider this xml:-

<AssessmentOrder>
<Assessment packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="2183404" language="MULTI" stage="1" jobLevel="Graduate/University" originalTimer="00:25:00" adjustedTimer="00:25:00"/>
<Report packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="1711998" language="en-US" comparisonGroup="" reportFormat="Pdf" reportMedia="Link"/>
<AssessmentAccess hasAssessments="true">
<Assessment stage="1">
</Assessment>
</AssessmentAccess>
</AssessmentOrder>
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Request_Name#Response" )

for( orderItemAssess in holder.getNodeValues( "//*:AssessmentOrder//*:Assessment/@orderItemRef" ))
log.info orderItemAssess
for( orderItemReport in holder.getNodeValues( "//*:AssessmentOrder//*:Report/@orderItemRef" ))
log.info orderItemReport

 Now try this code.

Oct21
Occasional Contributor

Re: Need to extract the value of an attribute from response XML

Hi 

 

Thanks for responding, but the code above is returning null value.

 

Thanks

Oct21

avidCoder
Super Contributor

Re: Need to extract the value of an attribute from response XML

Please refresh the page.. I have changed the solution.

nmrao
Community Hero

Re: Need to extract the value of an attribute from response XML

def str = '''<AssessmentOrder>
<Assessment packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="2183404" language="MULTI" stage="1" jobLevel="Graduate/University" originalTimer="00:25:00" adjustedTimer="00:25:00"/>
<Report packageTag="A_VAT_Induct" name="Verify - Inductive Reasoning (2007)" orderItemRef="1711998" language="en-US" comparisonGroup="" reportFormat="Pdf" reportMedia="Link"/>
<AssessmentAccess hasAssessments="true">
<Assessment stage="1">
</Assessment>
</AssessmentAccess>
</AssessmentOrder>'''

def  xml = new XmlSlurper().parseText(str)
def getOrderRef = { element ->
	xml.'**'.find {it.name() == element}.@orderItemRef
}

def elements = ['Assessment', 'Report']
elements.each { log.info getOrderRef(it) }

The above demonstrates with fixed xml.

In order to work for your response, assign the response to 'str' variable i.e., first line.



Regards,
Rao.
Oct21
Occasional Contributor

Re: Need to extract the value of an attribute from response XML

@nmrao 

I tried below code to save my response xml. In my case response xml will always generate new OrderRef.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def str = groovyUtils.getXmlHolder("RegistrationRequest#Response")
def xml = new XmlSlurper().parseText(str)
def getOrderRef = { element ->
xml.'**'.find {it.name() == element}.@orderItemRef
}

def elements = ['Assessment', 'Report']
elements.each { log.info getOrderRef(it) }

But am getting error. Could you please check?

 

Thanks,

Oct21

nmrao
Community Hero

Re: Need to extract the value of an attribute from response XML

Difficult to check without the details of the error.

By the way, you can try:

def str = context.expand('{RegistrationRequest#Response}')


Regards,
Rao.
nmrao
Community Hero

Re: Need to extract the value of an attribute from response XML

But have you tried with fixed xml? Was it doing as expected?


Regards,
Rao.
Oct21
Occasional Contributor

Re: Need to extract the value of an attribute from response XML

@nmrao  Yes, it worked fine with static XML.

Please see the screenshot for error details.

 

Thanks

Oct21

nmrao
Community Hero

Re: Need to extract the value of an attribute from response XML

Ok did you try the other reply?


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors