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:
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.
mkp.xmlDeclaration()
- adds<?xml version="1.0"?>
details.each { detail ->
- details is list. We want to loop thru the each detail. Each value goes intodetail
.Just similar to
for(detail : details)
.fld
is also the same as above.