Forum Discussion

yhkimingd's avatar
yhkimingd
New Contributor
16 years ago

Script to traverse in response to get data, then insert data to a request

Hi, a newbie in soapUI Pro 3.0.2.  I need to be able to parse through an XML response to check for a value, if true grab another value in the same type, and insert that data value in another request.  I believe Groovy Script can help me do this, but I need some pointers on which methods to use and proper syntax.  Here is what the response would look like:

         
           
               162
               Canceled
           

           
               177
               Approved
           

            ...[more data types]
         


If Status == "Approved", get the Number in DataType, else go to next Status.  Once I get the first Number with Approved Status, I need to insert it into the next request.  How would I get started to do this?  I am thinking that I would start by using the XmlHolder object and use the get/setNodeValue methods, but I am not sure how to set up the for loop and/or if statements.

Appreciate your help.

7 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Assuming you want the first "Approved" value, why not do use a PropertyTransfer step.

    Set the source step to be the initial request, the source property 'ResponseAsXML' and the XPath like this:

    [tt:35u4nv8f]declare namespace a='http://company.com/definition/2009/08/someservice';
    //DataTypes/a:DataType[a:Status='Approved']/a:Number[/tt:35u4nv8f]

    Make the target a Properties step, then use a property expansion to insert the value in the request XML of the next request.

    Make sure to check "Transfer Text Content" checkbox in the PropertyTransfer step.
  • yhkimingd's avatar
    yhkimingd
    New Contributor
    Thanks for the reply.  To clarify, I need to do a check for Status only, then get the Number value associated with that Status for the next request.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    That XPath will give you the Number value from within the first DataType that has Status = 'Approved', which seems to be what you want.

    I guess you would need additional logic if you could have a result where there was no Approved DataType.
  • yhkimingd's avatar
    yhkimingd
    New Contributor
    M McDonald wrote:

    That XPath will give you the Number value from within the first DataType that has Status = 'Approved', which seems to be what you want.

    I guess you would need additional logic if you could have a result where there was no Approved DataType.

    Yes, I would need additional logic because in most cases the first few will not have the Approved status.  Appreciate any help/pointers on implementing this logic.  Thanks.
  • yhkimingd's avatar
    yhkimingd
    New Contributor
    yhkimingd wrote:

    M McDonald wrote:

    That XPath will give you the Number value from within the first DataType that has Status = 'Approved', which seems to be what you want.

    I guess you would need additional logic if you could have a result where there was no Approved DataType.

    Yes, I would need additional logic because in most cases the first few will not have the Approved status.  Appreciate any help/pointers on implementing this logic.  Thanks.

    Sorry, I think I finally got what you meant.  I got it to work for the time being w/o using a script.  Thanks a lot!
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    If you don't get an Approved status you could use a Conditional GoTo step to change the flow of the script.

    Or, if you have a default value (say 0) that you wanted to use if no Approved items were found, you could put some logic in the PropertyTransfer:

    [tt:rwowkwqm]declare namespace a='http://company.com/definition/2009/08/someservice';
    if (exists(//DataTypes/a:DataType[a:Status='Approved']/a:Number))
    then //DataTypes/a:DataType[a:Status='Approved']/a:Number
    else 0[/tt:rwowkwqm]

    I think that works...