0

I have a sample LEDES XML file https://codebeautify.org/xmlviewer/cbdc79e7 and I am trying to create a Map with Invoice node's inv_id value as key and all of its child elements file_item_nbr values as below

['Invoice 31' : [10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33] 
 'Invoice 32' : [50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73] 
]

can someone please help me with it?

Rao
  • 20,781
  • 11
  • 57
  • 77
OTUser
  • 3,788
  • 19
  • 69
  • 127

1 Answers1

1

You should be able to get it with sudo code:

  • Use XmlSlurper to parse xml
  • Read all invoice elements
  • Build the required map

Here is the script:

//Not putting entire xml here, just pass the xml as string to parseText method
def xml = new XmlSlurper().parseText(xmlString)
//Get the invoices
def invoices = xml.'**'.findAll{it.name() == 'invoice'}
//Build the desired result
println invoices.collectEntries {inv -> [(inv.inv_id): inv.'**'.findAll{it.name() == 'file_item_nbr'}*.text()] }

You may quickly try it online demo

Rao
  • 20,781
  • 11
  • 57
  • 77
  • thanks for the solution it works, am wondering if is it possible to achieve the same from a JAX-B Java object which is generated from the XML? – OTUser Dec 08 '17 at 06:01
  • @RanPaul, I believe, it should be very much possible. But it may be entirely different. – Rao Dec 08 '17 at 06:29
  • created another question for Groovy solution https://stackoverflow.com/questions/47717505/groovy-create-a-map-with-jax-b-objects-specific-attributes can you please try to answer it? – OTUser Dec 08 '17 at 15:38