999888777662059 999888777662059 999888777662059

How to get data from xml file using groovy?

1.3k Views Asked by At

Following groovy script is not working as expected.

def xml="<Collection><CustomerQuote><ID>99988877766</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote><CustomerQuote><ID>99988877755</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote></Collection>"
    
 def completeXml= new XmlSlurper().parseText(xml);
 def IDs = completeXml.Collection.CustomerQuote.findAll{node-> node.name() == 'ID' }*.text();

I am trying to copy all the ID value in xml in the IDs

Output

IDs[]

Expected Output

IDs[99988877766,99988877755]

I am not sure what i am doing wrong here.

Can anyone guide.

Thank you Regards Prat

1

There are 1 best solutions below

0
Leonard Brünings On BEST ANSWER

The root node has to be omitted when using XmlSlurper, and you don't need to use findAll for this.

def xml="<Collection><CustomerQuote><ID>99988877766</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote><CustomerQuote><ID>99988877755</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote></Collection>"
    
def completeXml= new XmlSlurper().parseText(xml);
def IDs = completeXml.CustomerQuote.ID*.text();

Will output:

[
  "99988877766",
  "99988877755"
]

Try it in the Groovy Web Console