Groovy Script to read and transform xml

2019-09-14 20:52发布

问题:

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>

回答1:

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.