on 08-16-2016 5:37 PM
Hi Experts ,
I am facing some issues for one of my developments . I need to do the following with my input XML data -
1.Sort the data
2.Group all the data under a common header (<MT_Test_Output>) .
3.Add a node ( <Name>) after the common header for each group of data .
Note : The Input header tag <MT_Test> needs to be replaced by <MT_Test_Output> in the output .
Its pretty similar to one of my earlier posts mentioned below on Java Mapping only the Input header tag needs to be modified and a New node needs to be added .
Input :
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<ns1:MT_Test xmlns:ns1="http://test.com/xi/TEST">
<row>
<Seller_ID>1234</Seller_ID>
<SKU>SKU</SKU>
<Slave_Code>Slave_Code</Slave_Code>
<Quantity>Quantity</Quantity>
</row>
<row>
<Seller_ID>1234</Seller_ID>
<SKU>SKU1</SKU>
<Slave_Code>Slave_Code2</Slave_Code>
<Quantity>Quantity</Quantity>
</row>
<row>
<Seller_ID>4567</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
<row>
<Seller_ID>4568</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
<row>
<Seller_ID>4568</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
<row>
<Seller_ID>1234</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
</ns1:MT_Test>
</ns0:Message1>
</ns0:Messages>
-------------------------------------------------------------------------------
Expected Output :
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<ns1:MT_Test_Output xmlns:ns1="http://test.com/xi/TEST">
<name>1234_20160610</name>
<row>
<Seller_ID>1234</Seller_ID>
<SKU>SKU</SKU>
<Slave_Code>Slave_Code</Slave_Code>
<Quantity>Quantity</Quantity>
</row>
<row>
<Seller_ID>1234</Seller_ID>
<SKU>SKU1</SKU>
<Slave_Code>Slave_Code2</Slave_Code>
<Quantity>Quantity</Quantity>
</row>
<row>
<Seller_ID>1234</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
</ns1:MT_Test_Output>
<ns1:MT_Test_Output xmlns:ns1="http://test.com/xi/TEST">
<name>4567_20160610</name>
<row>
<Seller_ID>4567</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
</ns1:MT_Test_Output>
<ns1:MT_Test_Output xmlns:ns1="http://test.com/xi/TEST">
<name>4568_20160610</name>
<row>
<Seller_ID>4568</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
<row>
<Seller_ID>4568</Seller_ID>
<SKU/>
<Slave_Code/>
<Quantity/>
</row>
</ns1:MT_Test_Output>
</ns0:Message1>
</ns0:Messages>
Regards
Abhishek
Hi Abhishek,
Please try this code
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.NamedNodeMap;
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 RepeatNode1 extends AbstractTransformation{
Document docOut;
NodeList row=null;
Element r;
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
try {
execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());
} catch (Exception e) {
e.printStackTrace();
throw new StreamTransformationException(e.getMessage());
// TODO Auto-generated catch block
}
}
void sortandCreateNode(NodeList a,Element grandParent) throws StreamTransformationException
{
try
{
Element arr[]=new Element[a.getLength()];
for (int i = 0; i < arr.length; i++)
{
arr[i]=(Element) a.item(i);
}
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j].getElementsByTagName("Seller_ID").item(0).getTextContent().compareTo(arr[index].getElementsByTagName("Seller_ID").item(0).getTextContent())<0)
index = j;
}
String smallerNumber = arr[index].getElementsByTagName("Seller_ID").item(0).getTextContent();
arr[index].getElementsByTagName("Seller_ID").item(0).setTextContent(arr[i].getElementsByTagName("Seller_ID").item(0).getTextContent());
arr[i].getElementsByTagName("Seller_ID").item(0).setTextContent(smallerNumber);
}
Element newParent=null;
for(int i=0;i<arr.length;++i)
{
Node parentNode=arr[i].getParentNode();
if(i==0 || arr[i].getElementsByTagName("Seller_ID").item(0).getTextContent().compareToIgnoreCase(arr[i-1].getElementsByTagName("Seller_ID").item(0).getTextContent())!=0)
{
String val=arr[i].getElementsByTagName("Seller_ID").item(0).getTextContent()+"_20160610";
Node newNode=docOut.createElement("name");
newNode.setTextContent(val);
newParent=docOut.createElement(parentNode.getNodeName().replaceAll("MT_Test", "MT_Test_Output"));
NamedNodeMap attributes = parentNode.getAttributes();
for (int a1 = 0; a1 < attributes.getLength(); a1++) {
Node theAttribute = attributes.item(a1);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
newParent.setAttribute(theAttribute.getNodeName(), theAttribute.getNodeValue());
}
newParent.appendChild(newNode);
}
Node rowVal=docOut.importNode(arr[i], true);
newParent.appendChild(rowVal);
grandParent.appendChild(newParent);
}
}
catch(Exception e)
{
System.out.print(e.getClass().getName());
String error = "Probabbly missing Seller_ID node value";
if(e.getClass().getName().indexOf("NullPointerException")<0)
{
error="";
}
throw new StreamTransformationException(e.getMessage()+" error: "+e.getClass().getName()+" "+error);
}
return;
}
private void execute(InputStream in,
OutputStream out) throws StreamTransformationException{
// TODO Auto-generated method stub
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(in);
docOut = docBuilder.newDocument();
Node SourceRootNode=doc.getDocumentElement();
Element targetRootNode=docOut.createElement(SourceRootNode.getNodeName());
NamedNodeMap attributes = SourceRootNode.getAttributes();
for (int a1 = 0; a1 < attributes.getLength(); a1++) {
Node theAttribute = attributes.item(a1);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
targetRootNode.setAttribute(theAttribute.getNodeName(), theAttribute.getNodeValue());
}
docOut.appendChild(targetRootNode);
NodeList MT_Test=doc.getElementsByTagName("ns1:MT_Test");
for(int k=0;k<MT_Test.getLength();++k)
{
Element temp1=(Element) MT_Test.item(k);
Element grandParent=docOut.createElement(temp1.getParentNode().getNodeName());
attributes = temp1.getParentNode().getAttributes();
for (int a1 = 0; a1 < attributes.getLength(); a1++) {
Node theAttribute = attributes.item(a1);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
grandParent.setAttribute(theAttribute.getNodeName(), theAttribute.getNodeValue());
}
targetRootNode.appendChild(grandParent);
row=temp1.getElementsByTagName("row");
sortandCreateNode(row,grandParent);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(docOut);
StreamResult result = new StreamResult(out);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
in.close();
out.close();
return ;
}//try
catch (Exception e) {
e.printStackTrace();
throw new StreamTransformationException(e.getMessage());
// TODO Auto-generated catch block
}
}
}
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anupam ,
I tested the code in PI Operation Mapping Test environment using sample data and Java Mapping and it is working fine. So thank you for the effort .
But when I am running end-to-end , I am getting an error in PI Message Monitor as below :
"Mapping "http://test.com/xi/Test/Repeat_Node" failed to execute: MappingException: Split mapping created no messages, cannot proceed. Review your mapping setup: splitting to 0 messages is not allowed.
Transmitting the message to endpoint <local> using connection JDBC_http://sap.com/xi/XI/System failed, due to: com.sap.aii.af.service.mapping.MappingException: Split mapping created no messages, cannot proceed. Review your mapping setup: splitting to 0 messages is not allowed. "
I referred many posts, but couldn't get resolution to my issue. Some have suggested to remove unwanted tag like <messages> and <message1> in the xml . Can we remove the tags using Java Mapping in the Output ?
Regards
Abhishek
Hi Abhishek,
Every XML has to have a root element. Now here is the hierarchy
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"> 1...1
<ns0:Message1> 1.....unbounded
<ns1:MT_Test_Output> 0....unbounded
<row> 0....unbounded
If you remove "ns0:Messages" and message1 , there remains no root xml node to hold the document since MT_Test_output occurs multiple times. Thsi makes xml invalid.
Please provide details of mapping and screen shots so that the issue can be resolved.
Regards
Anupam
Hi Abhishek,
Please add a second java mapping shown below following the first one in the same OM.
This will remove the top nodes and push output to target. Please let me know if this works
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
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 removeTopNodes extends AbstractTransformation{
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream());
}
public void execute(InputStream in, OutputStream out) throws StreamTransformationException
{
try
{
StringBuilder s=new StringBuilder();
int c;
while((c=in.read())!=-1)
{
s.append((char)c);
}
int p=s.indexOf("<ns0:Message1>")+"<ns0:Message1>".length();
int p1=s.indexOf("</ns0:Message1>");
if(p>=0 && p1>p)
{
s=new StringBuilder(s.substring(p,p1));
}
out.write(s.toString().getBytes("UTF-8"));
// write the content into xml file
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
}
catch(Exception e)
{
e.printStackTrace();
throw new StreamTransformationException(e.getMessage());
}
}
}
Regards
Anupam
Please find the below Payload .
<?xml version="1.0" encoding="utf-8"?>
<ns:MT_Inventory_out_Test xmlns:ns="http://test.com/xi/TEST`">
<row>
<Seller_ID>Seller ID</Seller_ID>
<SKU>SKU</SKU>
<Slave_Code>Slave Code</Slave_Code>
<Quantity>Quantity</Quantity>
<MRP>MRP</MRP>
<MOP>MOP</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509228</SKU>
<Slave_Code>1610</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509228</SKU>
<Slave_Code>1568</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1572797008</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>30.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1573193004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>9.000</Quantity>
<MRP>995</MRP>
<MOP>995</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574311004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>1895</MRP>
<MOP>1895</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574311005</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>1895</MRP>
<MOP>1895</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574348004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>10.000</Quantity>
<MRP>695</MRP>
<MOP>695</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574348005</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>6.000</Quantity>
<MRP>695</MRP>
<MOP>695</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574511010</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>5.000</Quantity>
<MRP>1695</MRP>
<MOP>1695</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574791001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>1695</MRP>
<MOP>1695</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1574791002</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>1695</MRP>
<MOP>1695</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1687164001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP></MRP>
<MOP></MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1687187001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>500</MRP>
<MOP>500</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1687187002</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>500</MRP>
<MOP>500</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1687187003</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>3.000</Quantity>
<MRP>500</MRP>
<MOP>500</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1687187004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>4.000</Quantity>
<MRP>500</MRP>
<MOP>500</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1740631003</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP></MRP>
<MOP></MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1806148001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>7.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1806148002</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>6.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1806148003</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>7.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1806148004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>7.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1811171001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1001371001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>551.000</Quantity>
<MRP>4645</MRP>
<MOP>4645</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1001371901</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP></MRP>
<MOP></MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509223</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509224</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509227</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1004509228</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>7.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>27.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817002</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>96.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817003</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>44.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817004</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>48.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817005</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>63.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817006</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>17.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817007</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>5005.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817008</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1015817049</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>15.000</Quantity>
<MRP>3495</MRP>
<MOP>3495</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1032548012</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>128.000</Quantity>
<MRP>895</MRP>
<MOP>895</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1183252001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>3395</MRP>
<MOP>3395</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1183252002</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>3395</MRP>
<MOP>3395</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1183252005</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>3395</MRP>
<MOP>3395</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1490324017</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>4998.000</Quantity>
<MRP>995</MRP>
<MOP>995</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1539899008</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>1.000</Quantity>
<MRP>1095</MRP>
<MOP>1095</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1539899014</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>2.000</Quantity>
<MRP>1095</MRP>
<MOP>1095</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1572797001</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>9.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
<row>
<Seller_ID>100116</Seller_ID>
<SKU>1572797005</SKU>
<Slave_Code>1508</Slave_Code>
<Quantity>29.000</Quantity>
<MRP>100</MRP>
<MOP>100</MOP>
</row>
</ns:MT_Inventory_out_Test>
Regards
Abhishek
User | Count |
---|---|
85 | |
10 | |
9 | |
9 | |
6 | |
6 | |
6 | |
5 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.