使用 XML 的特定父节点值及其所有子节点值创建一个 Map
Create a Map with specific parent node value and all of its child node values of an XML
我有一个示例 LEDES XML 文件 https://codebeautify.org/xmlviewer/cbdc79e7,我正在尝试创建一个以 Invoice 节点的 inv_id
值作为键及其所有子元素 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]
]
有人可以帮我吗?
你应该可以通过 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
我有一个示例 LEDES XML 文件 https://codebeautify.org/xmlviewer/cbdc79e7,我正在尝试创建一个以 Invoice 节点的 inv_id
值作为键及其所有子元素 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]
]
有人可以帮我吗?
你应该可以通过 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