Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import groovy.xml.StreamingMarkupBuilder
- import groovy.xml.XmlSlurper
- import groovy.xml.XmlUtil
- 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>[email protected]</Email>
- <Address>123,acdf</Address>
- </RecordDetail>
- </Details>
- <Details>
- <RecID>2</RecID>
- <RecordDetail>
- <Name>ABC</Name>
- <Email>[email protected]</Email>
- </RecordDetail>
- </Details>
- </XYZ>
- </Record>'''
- def parsedXml = new XmlSlurper().parseText(xml)
- def builder = new 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 XmlUtil.serialize(transformedXml)
- //// output
- <?xml version="1.0" encoding="UTF-8"?><Record>
- <Header>
- <Code>
- <Code>12345</Code>
- </Code>
- </Header>
- <Details>
- <RecID>
- <RecID>1</RecID>
- </RecID>
- <RecordDetail>
- <FieldName>Name</FieldName>
- <FieldValue>ABC</FieldValue>
- </RecordDetail>
- <RecordDetail>
- <FieldName>Email</FieldName>
- <FieldValue>abc@in.com</FieldValue>
- </RecordDetail>
- <RecordDetail>
- <FieldName>Address</FieldName>
- <FieldValue>123,acdf</FieldValue>
- </RecordDetail>
- </Details>
- <Details>
- <RecID>
- <RecID>2</RecID>
- </RecID>
- <RecordDetail>
- <FieldName>Name</FieldName>
- <FieldValue>ABC</FieldValue>
- </RecordDetail>
- <RecordDetail>
- <FieldName>Email</FieldName>
- <FieldValue>abc@in.com</FieldValue>
- </RecordDetail>
- </Details>
- </Record>
- Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement