Groovy脚本来读取和转换XML [英] Groovy Script to read and transform xml
问题描述
需要Groovy脚本的帮助。我有以下输入xml,其中这个xml将被动态填充,并且我们没有任何有关在 RecordDetails
Node下填充多少节点的线索。
输入:
< ;?xml version =1.0encoding =UTF-8?>
< Record>
< XYZ>
< Header>
< Code> 12345< / Code>
< / Header>
<详情>
< RecID> 1< / RecID>
< RecordDetail>
<名称> ABC< /名称>
<电子邮件> abc@in.com< /电子邮件>
<地址> 123,acdf< /地址>
< / RecordDetail>
< /详细>
<详情>
< RecID> 2< / RecID>
< RecordDetail>
<名称> ABC< /名称>
<电子邮件> abc@in.com< /电子邮件>
< / RecordDetail>
< /详细>
< / XYZ>
< / Record>
输出:
<?xml version =1.0encoding =UTF-8?>
< Record>
< Header>
< Code> 12345< / Code>
< / Header>
<详情>
< RecID> 1< / RecID>
< RecordDetail>
< FieldName> NAME< / FieldName>
< FieldValue> ABC< / FieldValue>
< / RecordDetail>
< RecordDetail>
< FieldName>电子邮件< / FieldName>
< FieldValue> ABC@a.com< / FieldValue>
< / RecordDetail>
< /详细>
< / Record>
您只需要转换输入xml。
这可以通过以下方式实现:
看起来您正在寻找后者。 p>
以下是groovy脚本: $ b
def xml ='''<?xml version =1.0encoding =UTF-8?>
< Record>
< XYZ>
< Header>
< Code> 12345< / Code>
< / Header>
<详情>
< RecID> 1< / RecID>
< RecordDetail>
<名称> ABC< /名称>
<电子邮件> abc@in.com< /电子邮件>
<地址> 123,acdf< /地址>
< / RecordDetail>
< /详细>
<详情>
< RecID> 2< / RecID>
< RecordDetail>
<名称> ABC< /名称>
<电子邮件> abc@in.com< /电子邮件>
< / RecordDetail>
< /详细>
< / XYZ>
< / Record>'''
def parsedXml = new XmlSlurper()。parseText(xml)
$ b $ def builder = new groovy.xml.StreamingMarkupBuilder )
builder.encoding ='UTF-8'
def transformedXml = builder.bind {
mkp.xmlDeclaration()
记录{
标题{
code(parsedXml。'**'。find {it.name()=='Code'})
}
def details = parsedXml。'**'。findAll {it.name()= ='Details'}
details.each {detail - >
详细信息{
RecID(detail.RecID)
detail.RecordDetail.children()。each {fld - >
RecordDetail {
FieldName(fld.name())
FieldValue(fld.text())
}
}
}
}
println groovy.xml.XmlUtil.serialize(transformedXml)
可以在线快速尝试 演示 输出: 编辑:根据OP的问题。 与 Need help on Groovy Script. I have the following input xml where this xml will be dynamically populated and we do not have any clues on how many nodes will be populated under Input: Output:
You just need to transform the input xml. This can be achieved by: Looks like you are looking for the later one. Here is the groovy script: This can be quickly tried online Demo Output: EDIT: Based on the OP's questions. Just similar to 这篇关于Groovy脚本来读取和转换XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
mkp.xmlDeclaration()
- 添加<?xml version =1.0?>
details.each {detail - >
- details is list。我们想通过每个细节循环。每个值都进入 detail
。
类似(detail:details)
。
fld
也与上述相同。 RecordDetails
Node.<?xml version="1.0" encoding="UTF-8"?>
<Record>
<XYZ>
<Header>
<Code>12345</Code>
</Header>
<Details>
<RecID>1</RecID>
<RecordDetail>
<Name>ABC</Name>
<Email>abc@in.com</Email>
<Address>123,acdf</Address>
</RecordDetail>
</Details>
<Details>
<RecID>2</RecID>
<RecordDetail>
<Name>ABC</Name>
<Email>abc@in.com</Email>
</RecordDetail>
</Details>
</XYZ>
</Record>
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<Header>
<Code>12345</Code>
</Header>
<Details>
<RecID>1</RecID>
<RecordDetail>
<FieldName>NAME</FieldName>
<FieldValue>ABC</FieldValue>
</RecordDetail>
<RecordDetail>
<FieldName>Email</FieldName>
<FieldValue>ABC@a.com</FieldValue>
</RecordDetail>
</Details>
</Record>
def xml = '''<?xml version="1.0" encoding="UTF-8"?>
<Record>
<XYZ>
<Header>
<Code>12345</Code>
</Header>
<Details>
<RecID>1</RecID>
<RecordDetail>
<Name>ABC</Name>
<Email>abc@in.com</Email>
<Address>123,acdf</Address>
</RecordDetail>
</Details>
<Details>
<RecID>2</RecID>
<RecordDetail>
<Name>ABC</Name>
<Email>abc@in.com</Email>
</RecordDetail>
</Details>
</XYZ>
</Record>'''
def parsedXml = new XmlSlurper().parseText(xml)
def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def transformedXml = builder.bind {
mkp.xmlDeclaration()
Record {
Header {
Code (parsedXml.'**'.find{ it.name() == 'Code'})
}
def details = parsedXml.'**'.findAll{ it.name() == 'Details'}
details.each { detail ->
Details {
RecID (detail.RecID)
detail.RecordDetail.children().each { fld ->
RecordDetail {
FieldName (fld.name())
FieldValue (fld.text())
}
}
}
}
}
}
println groovy.xml.XmlUtil.serialize(transformedXml)
mkp.xmlDeclaration()
- adds <?xml version="1.0"?>
details.each { detail ->
- details is list. We want to loop thru the each detail. Each value goes into detail
.for(detail : details)
.fld
is also the same as above.