Forum Discussion

Adam_White's avatar
Adam_White
New Contributor
16 years ago

Help getting node value with groovy script

Hi,

I'm having trouble accessing a node value, I keep getting null returned.  I'm using the free version of SoapUI to prove what we need to do can be done, but I'm having trouble getting it to work.

The groovy script below is for a Mockservice.  I'm trying to access a value from the request and send back a response based on a node value.

The XML Request is:




ccmsUser
ccmsPassword



Y





2
1



10
1-I6TS

232432



27E1460B-A5B9-4082-9968-235AAE728C22





My groovy script is:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( mockRequest.requestContent )

//return holder.getNodeValue( "//wsse:Username" )
return holder.getNodeValue( "//ServiceProviderEnrolmentReference" )
def sper = holder.getNodeValue( "//ServiceProviderEnrolmentReference" )


if (sper == "1")
  return "Response 1"
else if (sper == "2")
  return "Response 2"
else
return "Response 1"

The line return holder.getNodeValue( "//wsse:Username" ) works and returns a response.  But holder.getNodeValue( "//ServiceProviderEnrolmentReference" ) returns null.  If I change the node to anything contained within the tags I get null.

Can you tell me what I'm doing wrong?

Thanks,
Adam

8 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Adam,

    this is probably namespace-related; please try first associating the required namespace with the holder:

    holder.namespaces["ns"] = "http://website.com/interfaces"
    log.info( holder["//ns:ServiceProviderEnrolmentReference"] )

    does that help?

    regards!

    /Ole
    eviware.com
  • Adam_White's avatar
    Adam_White
    New Contributor
    Awesome, that worked.  A lot better than my hacked way below of getting it.

    def requestString = request.toString()
    def length = requestString.size()
    def iStart = requestString.indexOf('ServiceProviderEnrolmentReference')
    iStart = iStart + 34
    def iEnd = requestString.lastIndexOf('ServiceProviderEnrolmentReference')
    iEnd = iEnd - 2
    def enrolmentId = requestString.substring(iStart, iEnd)

    Thanks.
  • zxsoap's avatar
    zxsoap
    Occasional Contributor
    Hi OLE,

    How can I get a value node (ALL account_ID) which's occuring more then one time (see below respone)? Ex, my response return more than one account_id. Now I'm only get just the first accountID on xml response. But, I need to get all? I want to return all account_id (14,68,22,41).

    Here's what I used:

    roovyUtils = new com.eviware.soapui.support.GroovyUtils(context )
    holder = groovyUtils.getXmlHolder ("getAccountPricingGroupsByPriceGroupID#Response")

    accid = holder.getNodeValue("//account_id")
    log.info (accid)


    Here's the response


     

     
         
           
               
                  Success
               

               
                 
                      14
                      3819178372
                      123
                    .......
                      68
                 

                 
                      17
                      6099471829
                      123
                      .......
                 

                 
                      22
                      6092357899
                      123
                      ....
                 

                 
                      41
                      0451471007
                      .......
                 

               

           

         

     


    Thanks,
    ZX
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    please try the

    holder.getNodeValues( ..xpath.. )

    method instead, which returns an array of strings, one for each node matched by the specified xpath

    regards!

    /Ole
    eviware.com
  • Hi,
    I am trying to get all node values (NAME)using the following,
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
    def responseHolder = groovyUtils.getXmlHolder(messageExchange.responseContent);

    String[] names = responseHolder.getNodeValues("//ns1:findAllOwnersResponse/ns1:out/ns2:Owner/ns2:name");

    but still m getting null.. any one please help me here..
    my response looks like,


     
         
           
               
                  10000000
                  MacDonald, John
                  0
               

               
                  10000011
                  Fitzgerald, Thomas
                  0
               

                           
                  10000061
                  Brown, Brad
                  0
               

               
                  10000062
                  Begin, Steve
                  0
               

               
                  10000063
                  MacDonald, Craig
                  0
               

               
                  10000064
                  Greaves, James
                  0
               

               
                  10000065
                  Hurst, Geoff
                  0
               

               
                  10000066
                  Moore, Robert
                  0
               

               
                  10000067
                  Beckenbauer, Franz
                  0
               

               
                  10000068
                  Breitner, Paul
                  0
               

               
                  10000069
                  Klinsman, Jurgen
                  0
               

               
                  10000070
                  Meazza, Giuseppe
                  0
               

               
                  10000071
                  Piola, Silvio
                  0
               

               
                  10000072
                  Rivera, Gianni
                  0
               

               
                  10000073
                  Branch Managers
                  1
               

               
                  10000074
                  District Supervisors
                  1
               

               
                  10000075
                  Audit Committee
                  1
               

               
                  10000080
                  Brown, Monica
                  0
               

           

         

     
  • Looks like a namespace problem.  Does this Groovy interface provide a way to register prefix/namespace mappings?  If so, register a non-empty prefix associated with the "http://website.com/interfaces" namespace and use that prefix in the xpath query.  Otherwise, change the query to use "local-name()" references, like this:

      //*[local-name()='ServiceProviderEnrolmentReference']

    You'll see how gross this gets once your xpath query gets even a little longer than this.  If there's a way to register prefix/namespace mappings, do it.
  • These replies have been moved.