Hi Rashmi,
You can use below java mapping, instead of hard coding the target xml you can use below code to generate the target xml, and also it works for multiple records.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class ConcatJavaMap extends AbstractTransformation { public static void main(String[] args) throws StreamTransformationException, FileNotFoundException { ConcatJavaMap concat = new ConcatJavaMap(); FileInputStream input = new FileInputStream("input.xml"); FileOutputStream output = new FileOutputStream("output.xml"); concat.execute(input, output); } @Override public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException { execute(transformationInput.getInputPayload().getInputStream(), transformationOutput.getOutputPayload().getOutputStream()); } public void execute(InputStream inputStream, OutputStream outputStream) throws StreamTransformationException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); factory.setIgnoringElementContentWhitespace(true); Document inputDoc = builder.parse(inputStream); Document outputDoc = builder.newDocument(); Element outMsgType = outputDoc.createElement("ns0:MT_RJava_Concanat"); outMsgType.setAttribute("xmlns:ns0", "urn:javaMapping.com/Rashmi"); outputDoc.appendChild(outMsgType); NodeList recordsNodeList = inputDoc.getElementsByTagName("Records"); for (int i = 0; i < recordsNodeList.getLength(); i++) { Node recordsNode = recordsNodeList.item(i); if (recordsNode.getNodeType() == Node.ELEMENT_NODE) { Element recordsSource = (Element) recordsNode; Element recordsTarget = outputDoc.createElement("Records"); outMsgType.appendChild(recordsTarget); // Employee ID Element empElement = outputDoc.createElement("EmployeeID"); empElement.setTextContent(recordsSource.getElementsByTagName("EmployeeID").item(0).getTextContent()); recordsTarget.appendChild(empElement); // Result Element result = outputDoc.createElement("RESULT"); String name = recordsSource.getElementsByTagName("Emp_Name").item(0).getTextContent(); String surname = recordsSource.getElementsByTagName("Emp_Surname").item(0).getTextContent(); String fullName = name + " " + surname; result.setTextContent(fullName); recordsTarget.appendChild(result); } } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); DOMSource source = new DOMSource(outputDoc); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); } catch (Exception e) { getTrace().addDebugMessage(e.getMessage()); throw new StreamTransformationException(e.getMessage()); } }
}Result:
Regards,
Praveen.
