Forum Discussion

ktjrajesh's avatar
ktjrajesh
New Contributor
6 years ago

Facing problem parsing XML

Whomsoever it may concern

Please help in solving this issue.

 I am able to get the response but not able to get node values.

 

Groovy Script:

 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def requsetHolder = groovyUtils.getXmlHolder( "Define - Request#Request")

def responseContent = testRunner.testCase.getTestStepByName("MockResponseDefine").getPropertyValue("response")
def responseHolder = groovyUtils.getXmlHolder("Define - Request#Response")

log.info(responseHolder.getPrettyXml())


log.info(responseHolder["count(//DefineResponse/DefineResult/Word)"])
// loop item nodes in response message
for( item in responseHolder.getNodeValues( "//DefineResponse/DefineResult/Word" ))
log.info "\nItem : [$item]"

 

Output:

 

Wed Mar 21 15:46:55 IST 2018:INFO:<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<DefineResponse xmlns="http://services.aonaware.com/webservices/">
<DefineResult>
<Word>Hello</Word>
<Definitions>
<Definition>
<Word>Hello</Word>
<Dictionary>
<Id>gcide</Id>
<Name>The Collaborative International Dictionary of English v.0.44</Name>
</Dictionary>
<WordDefinition>Hello \Hel*lo"\, interj. &amp; n.
An exclamation used as a greeting, to call attention, as an
exclamation of surprise, or to encourage one. This variant of
{Halloo} and {Holloo} has become the dominant form. In the
United States, it is the most common greeting used in
answering a telephone.
[1913 Webster +PJC]</WordDefinition>
</Definition>
<Definition>
<Word>Hello</Word>
<Dictionary>
<Id>wn</Id>
<Name>WordNet (r) 2.0</Name>
</Dictionary>
<WordDefinition>hello
n : an expression of greeting; "every morning they exchanged
polite hellos" [syn: {hullo}, {hi}, {howdy}, {how-do-you-do}]</WordDefinition>
</Definition>
</Definitions>
</DefineResult>
</DefineResponse>
</soap:Body>
</soap:Envelope>

 

 

Wed Mar 21 15:46:55 IST 2018:INFO:0

4 Replies

    • ktjrajesh's avatar
      ktjrajesh
      New Contributor

      Sorry for reverting late, was not well and couldnt work on this earlier.

       

      Please find groovy script below which i tried, it says 0. Can you please help fixing the namespace for the response posted in original query.

       

      def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

      def requsetHolder = groovyUtils.getXmlHolder( "Define - Request#Request")
      def responseHolder = groovyUtils.getXmlHolder("Define - Request#Response")

      log.info(responseHolder.getPrettyXml())

      // define namespace
      responseHolder.namespaces["ns1"] = "http://services.aonaware.com/webservices/"
      responseHolder.namespaces["soap"] = "http://schemas.xmlsoap.org/soap/envelope/"


      log.info(responseHolder["count(//WordDefinition)"])

  • ktjrajesh's avatar
    ktjrajesh
    New Contributor

    Can someone help me with this please. Thanks. 

    Not sure if i am defining the namespace(if requried)

  • JHunt's avatar
    JHunt
    Community Hero

    Given a String xml as your XML above...

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def responseHolder = groovyUtils.getXmlHolder(xml)
    responseHolder.declareNamespace('bla', "http://services.aonaware.com/webservices/")
    assert responseHolder.getNodeValues( "//bla:DefineResponse/bla:DefineResult/bla:Word" ) == ['Hello']
    assert responseHolder["count(//bla:WordDefinition)"] == '2'