我有一个示例LEDES XML文件https://codebeautify.org/xmlviewer/cbdc79e7,我正在尝试使用Invoice节点的inv_id
值作为键创建一个Map,其所有子元素file_item_nbr
值如下所示
['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]
]
有人可以帮我吗?
答案 0 :(得分:1)
您应该可以使用sudo代码获取它:
XmlSlurper
解析xml invoice
元素这是脚本:
//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()] }
您可以快速在线试用 demo