Forum Discussion

Bryan_Nahrwold's avatar
Bryan_Nahrwold
Contributor
16 years ago

Reparsing an xml string with groovy

I have the following a WSDL operation response:


 
     
        <activation id="c5fb-7bfc-a755-4f07-9967" returnCode="success" description="" name="Avocent Management Platform Server" version="4.0"/><activation id="67b9-8372-eb53-4f8c-a863" returnCode="success" description="" name="Avocent Management Platform Console - 500 pack" version="4.0"/><activation id="5204-4734-c59e-4444-b734" returnCode="success" description="" name="Avocent Management Platform Reporting" version="4.0"/>
     

 


I am trying to verify that each of the three activation tags contains returnCode="success".  Because I have to reparse the string content of the out tag, I have been trying to use the following groovy script:

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

holder = groovyUtils.getXmlHolder("activateLicenses#Response")
log.info(activationOut = holder.getNodeValue( "//out" ))

This produces the response:
Thu Apr 29 16:24:14 MDT 2010:INFO:

I beleive my next step is to reparse using:
log.info(activationNode = groovyUtils.getXmlHolder(activationOut))


However, this give me an Error dialog: java.lang.NullPointerException

What call do I need to use to view this returned string as a node and what would be the code line to obtain the value of one of the attributes returnCode for an activation tag?

9 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    The value for activationOut is not valid XML (3 separate nodes:)

    <activation id="c5fb-7bfc-a755-4f07-9967" returnCode="success" description="" name="Avocent Management Platform Server" version="4.0"/>
    <activation id="67b9-8372-eb53-4f8c-a863" returnCode="success" description="" name="Avocent Management Platform Console - 500 pack" version="4.0"/>
    <activation id="5204-4734-c59e-4444-b734" returnCode="success" description="" name="Avocent Management Platform Reporting" version="4.0"/>


    You would need to wrap it in an outer element.
  • Thanks for the feedback.  The string I am getting back is the content of the out tag. As a test of the recommendation I received, I tried:

    log.info(activationNode = groovyUtils.getXmlHolder(''))

    I get the same java.lang.NullPointerException.

    I've also tried:
    log.info(activationOut = holder.getNodeValue( "//out//activation[1]/@id" ))
    but it returns null
    I'm just don't know how to extract this information.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Sorry, there is not node value for the activation nodes as they are empty (only attributes.)

    Does this work for the IDs, for example?

    def ids = groovyUtils.getXmlHolder("<out>" + activationOut + "</out>")
    ids.getNodeValues("//activation/@id").each { id ->
    log.info id
    }
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Or this for all attributes:

    def outCount = activationOut.getDomNodes("//activation").size()
    for (def i = 1; i <= outCount; i++) {
    activationOut.getDomNodes("//activation[$i]/@*").each { attr  ->
    log.info "$attr.name[$i]:$attr.value"
    }
    }
  • Alas, no, but thanks for the suggestions.  Both:

    def outCount = activationOut.getDomNodes("//activation").size()

    and


    def ids = groovyUtils.getXmlHolder("" + activationOut + "")

    result in the NullPointerException.
  • Hi!

    hmm.. do you have a stacktrace for this npe in the error log?

    Are you using the latest version?

    regards!

    /Ole
    eviware.com
  • Let's tr another approach. I originally wanted to just use a property transfer, but the response is escaped:


     
         
            <activation id="a724-af8a-d172-4720-8605" returnCode="success" description="" name="Avocent Management Platform Server" version="4.0"/><activation id="5607-390a-7b73-42ff-ace2" returnCode="success" description="" name="Avocent Management Platform Console - 500 pack" version="4.0"/><activation id="2626-c91a-22a4-4cf5-bd38" returnCode="errcode.licensing.error.activate.invalid.activationid" description="Flexera Error: major.minor.sys (50040.41147.14)    License generator error: No component(s): file C:\WINDOWS\TEMP\vcg36457.prd line 12
    [Incident# 0050-82906] operationName=sHandleActivation. flxActSvrActivationSend failed. activationID=2626-c91a-22a4-4cf5-bd38 CommServer=https://fno-test.avocent.com:8888/flexnet/services/ActivationService?wsdl" name="Avocent Management Platform Reporting" version="4.0"/>

         

     


    I'm doing a multistep proeprty transfer.
    1st step:
    Source: activateLicenses  Property: response
    declare namespace ns2='http://ws.licensing.amp.avocent.com';
    //ns2:activateLicensesResponse/out

    Target: Properties  property responseXML

    produces:
    []

    I then create a second transfer:
    source: Properties  Property: responseXML
    I want to get the ID attribute from each activation tag, however
    /activation[1]/@id

    results in:
    [error: Unexepected element: EMPTY_TAG]

    I also tried:
    /activation[0]/@id just to make sure this wasn't indexed from 0, no difference.

    I know the response out tag is escaped. Is there a way to grab the id attribute from each
    activation tag even though this appears to be text now?

    Would appreciate any help you can offer.