Forum Discussion

Kevin_Slade's avatar
Kevin_Slade
Contributor
17 years ago

Transfer all elements of an XML array response into a list array for later use

The attached XML response file has an array of productOfferings. I need to extract each productOffering as a groovy list, where each element of the productOffering item is an item in the list.

This will be used later  get a single product offering for use in a subsequent call.

I have been looking using groovy to walk the array but appear to be getting empty results returned. Looking at the result with the outline view shows the items are populated.
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Kevin,

    Exactly what do you want to store in the list? The XML-text of each ProductOfferingSummary? Or its corresponding DOM Element? or some value in the XML?

    regards!

    /Ole
    eviware.com
  • Hi Ole

    The intention is to store all of the returned data content, so that a decision can be made later based on the type of product offering. The type of product offering being used later will determine which type of data class overload will be needed in a subsequent SOAP call.

    As you can see this relates the defect I raised that you are working on, SoapUI Pro Does not provide access to data classes that extend a base class.

    Thank you for all of your valuable assistance.

    Regards Kevin
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    ok.. hmm.. the following script will store the xml-string for each item:


    // get holder for response
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder( "Test Request#Response" )

    // select nodes
    holder.namespaces["ns"] = "http://EB.telecom.co.nz/"
    def nodes = holder.getDomNodes( "//ns:ProductOfferingSummary" )

    // create list of XML strings
    def list = []

    for( node in nodes )
    {
      java.io.StringWriter writer = new java.io.StringWriter()
      com.eviware.soapui.support.xml.XmlUtils.serialize( node, writer )
      list.add( writer.toString() )
    }

    // some debug info..
    log.info( list )
    log.info( list.size() )

    // store in context for later access
    context.nodeList = list



    Could this be a start?

    regards!

    /Ole
    eviware.com
  • Hello Ole

    As usual you have a great solution. I will implement this when I get into the office.

    Many Thanks

    Kevin
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Kevin,

    ok! Let me know how it works. The tricky bit might be to later parse these xml-strings to extract the desired values.. let me know if you need any help with this (or any other approach)!

    regards,

    /Ole
    eviware.com
  • Hello Ole

    The simple change to point to the test step that I need to analyse has allowed me to collect the xml into an array of lists.

    Thank you very much.

    If I need pointers on parsing the XML I will be sure to ask.

    Thank you for your usual prompt and accurate replies. This is certainly helping the cause to get more licenses installed here.

    Regards Kevin
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Kevin,

    well, that's definitely for a good cause :-)

    regards!

    /Ole
    eviware.com
  • tmulders's avatar
    tmulders
    Occasional Contributor
    Hello,

    I got basically the same problem. I receive an XML response with an array of Notifications. I need the id's of all Notifications and write them to a file so I can use it as a DataSource for other operations. Your groovy script gave me the id's within an xml string. Thats the problem, I dont need the XML string, I need the id's

    Is there way to do it? Thanks in advance.
    Kind Regards,
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    try changing the for loop to

    for( node in nodes )
    {
      def value = com.eviware.soapui.support.xml.XmlUtils.getNodeValue( node )
      list.add( value)
    }

    The list should now contain the text contents of the selected nodes..

    Hope this helps!

    regards,

    /Ole
    eviware.com