Hi Experts,
I am working on the below Java Mapping:
Source XML Structure:
<?xml version="1.0" encoding="UTF-8"?>
<ns9:POCombined xmlns:ns9="http://sap.com/xi30/mapping/patterns">
<Header>
<Name>myName</Name>
<Address>myAddress</Address>
</Header>
<Detail>
<Supplier>OfficeMax</Supplier>
<ProductID>11</ProductID>
<Quantity>12</Quantity>
<Price>13</Price>
</Detail>
<Detail>
<Supplier>OfficeDepot</Supplier>
<ProductID>21</ProductID>
<Quantity>22</Quantity>
<Price>23</Price>
</Detail>
<Detail>
<Supplier>OfficeDepot</Supplier>
<ProductID>31</ProductID>
<Quantity>32</Quantity>
<Price>33</Price>
</Detail>
<Detail>
<Supplier>OfficeMax</Supplier>
<ProductID>42</ProductID>
<Quantity>42</Quantity>
<Price>43</Price>
</Detail>
</ns9:POCombined>
Required Target XMl Structure:
<?xml version="1.0" encoding="UTF-8"?>
<ns9:POSplit xmlns:ns9="http://sap.com/xi30/mapping/patterns">
<Orders>
<Header>
<toCompany>OfficeMax</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
</Header>
<Items>
<productNo>11</productNo>
<quantity>12</quantity>
<price>13</price>
</Items>
<Items>
<productNo>42</productNo>
<quantity>42</quantity>
<price>43</price>
</Items>
</Orders>
<Orders>
<Header>
<toCompany>OfficeDepot</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
</Header>
<Items>
<productNo>21</productNo>
<quantity>22</quantity>
<price>23</price>
</Items>
<Items>
<productNo>31</productNo>
<quantity>32</quantity>
<price>33</price>
</Items>
</Orders>
</ns9:POSplit>
Java Mapping Program:
import java.io.FileInputStream;
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.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
public class PO_JavaMapping111 extends AbstractTransformation {
public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
try
{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builderel=factory.newDocumentBuilder();
/*input document in form of XML*/
Document docIn=builderel.parse(in);
/*document after parsing*/
Document docOut=builderel.newDocument();
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transform=tf.newTransformer();
Element root,child,child1,child2,child11,child12,child13,child21,child22,child23;
Node textChild11,textChild12,textChild13,textChild21,textChild22,textChild23;
Node toCompany,custName,custAddress,productNo,quantity,price;
String toCompanys,custNames,custAddresss,productNos,quantitys,prices;
root=docOut.createElement("ns9:POSplit");
root.setAttribute("xmlns:ns9","urn:java_mapping2");
NodeList nList = docIn.getElementsByTagName("Header");
child=docOut.createElement("Orders");
child1=docOut.createElement("Header");
child2=docOut.createElement("Items");
for (int temp = 0; temp < nList.getLength(); temp++)
{
/** toCompany Node **/
child11=docOut.createElement("toCompany");
toCompany=docIn.getElementsByTagName("Supplier").item(temp);
toCompanys=toCompany.getFirstChild().getNodeValue();
child1.appendChild(child11);
/** custName Node **/
child12=docOut.createElement("custName");
custName=docIn.getElementsByTagName("Name").item(temp);
custNames=custName.getFirstChild().getNodeValue();
child1.appendChild(child12);
/** custAddress Node **/
child13=docOut.createElement("custAddress");
custAddress=docIn.getElementsByTagName("Address").item(temp);
custAddresss=custAddress.getFirstChild().getNodeValue();
child1.appendChild(child13);
child.appendChild(child1);
child.appendChild(child2);
textChild11=docOut.createTextNode(toCompanys);
child11.appendChild(textChild11);
textChild12=docOut.createTextNode(custNames);
child12.appendChild(textChild12);
textChild13=docOut.createTextNode(custAddresss);
child13.appendChild(textChild13);
root.appendChild(child);
}
NodeList nList1 = docIn.getElementsByTagName("Detail");
for (int temp1 = 0; temp1 < nList1.getLength(); temp1++)
{
/** productNo Node **/
child21=docOut.createElement("productNo");
productNo=docIn.getElementsByTagName("ProductID").item(temp1);
productNos=productNo.getFirstChild().getNodeValue();
child2.appendChild(child21);
/** productNo Node **/
child22=docOut.createElement("quantity");
quantity=docIn.getElementsByTagName("Quantity").item(temp1);
quantitys=quantity.getFirstChild().getNodeValue();
child2.appendChild(child22);
/** price Node **/
child23=docOut.createElement("price");
price=docIn.getElementsByTagName("Price").item(temp1);
prices=price.getFirstChild().getNodeValue();
child2.appendChild(child23);
textChild21=docOut.createTextNode(productNos);
child21.appendChild(textChild21);
textChild22=docOut.createTextNode(quantitys);
child22.appendChild(textChild22);
textChild23=docOut.createTextNode(prices);
child23.appendChild(textChild23);
}
docOut.appendChild(root);
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
{
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
I am getting the XML Ouput as :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns9:POSplit xmlns:ns9="urn:java_mapping2">
<Orders>
<Header>
<toCompany>OfficeMax</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
<toCompany>OfficeMax</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
<toCompany>OfficeMax</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
<toCompany>OfficeMax</toCompany>
<custName>myName</custName>
<custAddress>myAddress</custAddress>
</Header>
<Items>
<productNo>11</productNo>
<quantity>12</quantity>
<price>13</price>
<productNo>21</productNo>
<quantity>22</quantity>
<price>23</price>
<productNo>31</productNo>
<quantity>32</quantity>
<price>33</price>
<productNo>42</productNo>
<quantity>42</quantity>
<price>43</price>
</Items>
</Orders>
</ns9:POSplit>
Please check the Required Output XML and do the needful.
Regards,
GIRIDHAR