Groovy脚本来读取和转换XML [英] Groovy Script to read and transform xml

查看:318
本文介绍了Groovy脚本来读取和转换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。



这可以通过以下方式实现:


  • 编写一个xslt并使用runner执行

  • 在groovy本身中进行转换。



看起来您正在寻找后者。 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)

可以在线快速尝试 演示
$ b

输出:

i.stack.imgur.com/IVz1H.pngrel =nofollow noreferrer>


编辑:根据OP的问题。

mkp.xmlDeclaration() - 添加<?xml version =1.0?>



details.each {detail - > - details is list。我们想通过每个细节循环。每个值都进入 detail



类似(detail:details)



fld 也与上述相同。

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 RecordDetails Node.

Input:

<?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>

Output:

<?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>

解决方案

You just need to transform the input xml.

This can be achieved by:

  • write an xslt and use runner to execute
  • transform in groovy itself.

Looks like you are looking for the later one.

Here is the groovy script:

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)

This can be quickly tried online Demo

Output:

EDIT: Based on the OP's questions.

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.

Just similar to for(detail : details).

fld is also the same as above.

这篇关于Groovy脚本来读取和转换XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆