Chirag Shah
Chirag Shah

Reputation: 55

Groovy XML Holder returns null for unformatted xml

I am retrieving xml tag using xmlholder but it's not working with unformatted xml.

def holder = groovyUtils.getXmlHolder(mockRequest.requestContent);
holder.declareNamespace("ns2", "http://example.com")

if(holder.getNodeValue('//ns2:GetCustomerInfo')!=null){
println true
}

I am getting true for formatted xml:

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
   <S:Header>
   </S:Header>
   <S:Body>
      <ns2:GetCustomerInfo xmlns:ns2="http://example.com">
         <ns2:Identifier>4111119876543210</ns2:Identifier>
      </ns2:GetCustomerInfo>
   </S:Body>
</S:Envelope>

I am not getting true if xml is unformatted and given as one line string.

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header></S:Header><S:Body><ns2:GetCustomerInfo xmlns:ns2="http://example.com"><ns2:Identifier>4111119876543210</ns2:Identifier></ns2:GetCustomerInfo></S:Body></S:Envelope>

Actually I need to retrieve value from unformatted xml as I am going to get data as unformatted.

Upvotes: 0

Views: 701

Answers (1)

SiKing
SiKing

Reputation: 10329

Your code is working as expected!

  1. In, what you are calling, the "formatted case" the value of the node GetCustomerInfo is a newline character and some whitespace, and so it is not null.
  2. In your "unformatted case", the value of the node GetCustomerInfo is nothing, and so it is null.

To prove this, you can insert a single space character in the unformatted string after the GetCustomerInfo node, and run your test again.

You might want to try using getDomNode() instead of getNodeValue() to get the behaviour you are looking for.

Upvotes: 2

Related Questions