Forum Discussion

Vijayata's avatar
Vijayata
Occasional Contributor
15 years ago

Parsing XML to get the node value

Hi,

I have simple xml like this:
<Response>
<e>
<cloudClass>Some Class name</cloudClass>
<errorCode>503</errorCode>
<errorDescription>Some Error</errorDescription>
<guid>ca1f535d-cd9f-409d-a66c-4e2c6fdf4321</guid>
</e>
</Response>

I have to parse this so as to get the guild value.
Can please someone guide me for this?

Thanks,
Vijayata

9 Replies

  • kamahade's avatar
    kamahade
    Regular Contributor
    Hi ya..

    Either you write perl script to parse it ( I have done this - Easy)

    Or

    Groovy script within SoapUI to do it.

    I don;t know to do it in Groovy..so I managed to do it in Perl.
  • Hi,

    here is your script

    def myfile="""<Response>
    <e>
    <cloudClass>Some Class name</cloudClass>
    <errorCode>503</errorCode>
    <errorDescription>Some Error</errorDescription>
    <guid>ca1f535d-cd9f-409d-a66c-4e2c6fdf4321</guid>
    </e>
    </Response>"""


    def response = new XmlSlurper().parseText(myfile)
    println response.e.guid


    regards
    nebojsa
    eviware.com
  • Vijayata's avatar
    Vijayata
    Occasional Contributor
    Ok Cool!!!
    That really worked for me.
    I am thankful to you

    Actually what if I have such repeating nodes and I want to loop through it and print the guids one by one on console? How can I do this?

    i.e.
    <Response>
    <e>
    <cloudClass>Some Class name</cloudClass>
    <errorCode>503</errorCode>
    <errorDescription>Some Error</errorDescription>
    <guid>ca1f535d-cd9f-409d-a66c-4e2c6fdfadf4</guid>
    </e>
    <e>
    <cloudClass>Some Class name</cloudClass>
    <errorCode>403</errorCode>
    <errorDescription>Some Error</errorDescription>
    <guid>dfgdg35d-cd9f-409d-a66c-4e2c6fdfaa31</guid>
    </e>
    <e>
    <cloudClass>Some Class name</cloudClass>
    <errorCode>404</errorCode>
    <errorDescription>Some Error</errorDescription>
    <guid>asdv535d-cd9f-409d-a66c-4e2c6fdf5678</guid>
    </e>
    </Response>
  • Vijayata's avatar
    Vijayata
    Occasional Contributor
    The first hint and also from some help from Google, I could solve my own problem.

    The way to loop through and get the guids is:

    def myfile="""<Response>
    <e>
    <cloudClass>Some Class name</cloudClass>
    <errorCode>503</errorCode>
    <errorDescription>Some Error</errorDescription>
    <guid>ca1f535d-cd9f-409d-a66c-4e2c6fdf4321</guid>
    </e>
    </Response>"""
    // OR you this can be the response from one step

    def response = new XmlSlurper().parseText(myfile)

    response.e.each {
    log.info "GUID :: $it.guid"
    }

    Thanks!!!
  • kamahade's avatar
    kamahade
    Regular Contributor
    Thanks for replying back to forum.. only like this we could improve the quality of this forum..rather than only Questions !

    Let me take this change to post my groovy script solution as well

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
    def responseHolder = groovyUtils.getXmlHolder("TESTCASE#Response");
    responseHolder.declareNamespace( 'se', 'http://WEBSITE_FOR_NAME_SPACE')

    MA_UUID=responseHolder.getNodeValue("//se:firstnodee[1]/nodenode[1]/whichnodeIwant[1]");


    Please tell me what is XmlSlurper