on 01-24-2012 4:17 PM
Hi All
I have been looking for a good example of a java mapping using the 7.1 API, which can be debugged in standalone NWDS using local files.
Can anyone suggest how [this example Java Mapping PI7.1 |http://wiki.sdn.sap.com/wiki/display/XI/JavamappingwithDOMandSAXparsersinnewmappingAPI%28PI+7.1%29] could be reworked using [Stefan Grube's debugging template|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/4774] [original link is broken] [original link is broken] [original link is broken]; for PI7.1?
Thanks in advance
>>which can be debugged in standalone NWDS using local files.
yes its possible...u just have to add static void main (as sugested in the blog) ..
Note: i dont have a system as of now..so i guess there can be some sysntax errors. Please check.
create input xml file with name "input.xml" and place it under "D" drive (it can be any location)
package prasad.NewAPI;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
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;
/*
* @author Prasad Nemalikanti
*/
public class NewAPIJavaMapping extends AbstractTransformation {
public static void main(String[] args) {
try{
NewAPIJavaMapping javaMapping =new NewAPIJavaMapping();
FileInputStream in=new FileInputStream("D:\\input.xml");
FileOutputStream out=new FileOutputStream("D:\\output.xml");
javaMapping.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
String result="";
/* The transform method which must be implemented */
public void transform(TransformationInput in,TransformationOutput out) throws StreamTransformationException
{
InputStream instream = in.getInputPayload().getInputStream();
// String for constructing target message structure
String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:newapi-javamapping\">");
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(instream);
traversingXML(doc);
}
catch(Exception e){}
fresult = fresult.concat(result);
fresult = fresult.concat("</ns0:MT_Customer>");
try{
out.getOutputPayload().getOutputStream().write(fresult.getBytes());
/* assigning the created target message to "TransformationOutput"*/
}
catch(IOException e1){}
}
/*DOM Parser */
public void traversingXML(Node node)
{
NodeList children = node.getChildNodes();
for(int i=0;i<children.getLength();i++)
{
Node child = children.item(i);
short childType = child.getNodeType();
if(childType==Node.ELEMENT_NODE)
{
String nodeName=child.getNodeName();
String targetNodeName=null;
if(nodeName.equals("Users"))
targetNodeName="Customers";
else if(nodeName.equals("ID"))
targetNodeName="CustomerID";
else if(nodeName.equals("UserName"))
targetNodeName="Name";
else if(nodeName.equals("City"))
targetNodeName="City";
else if(nodeName.equals("State"))
targetNodeName="State";
else if(nodeName.equals("Country"))
targetNodeName="Country";
if(targetNodeName!=null)
result=result.concat("<"+targetNodeName+">");
traversingXML(child);
if(targetNodeName!=null)
result=result.concat("</"+targetNodeName+">");
}
else if(childType==Node.TEXT_NODE)
{
String nodeValue = child.getNodeValue();
result = result.concat(nodeValue);
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks AmitSri. There seems to be a couple of problems with the code.
I changed the two lines below:
// FileInputStream in = new FileInputStream("D:
input.xml");
InputStream in = new FileInputStream(new File("D:
input.xml"));
// FileOutputStream out = new FileOutputStream("D:
output.xml");
FileOutputStream out = new FileOutputStream(new File("D:
output.xml"));
But there is a problem with:
javaMapping.execute(in, out) -
> Method execute undefined
Could you suggest the required changes please?
Thanks again.
Hi,
Could you please try this code
package prasad.NewAPI;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
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;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/*
* @author Prasad Nemalikanti
*/
public class NewAPIJavaMapping extends AbstractTransformation {
String result="";
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
NewAPIJavaMapping genFormat=new NewAPIJavaMapping();
FileInputStream in=new FileInputStream("C:\\Apps\\my folder\\sdn\\copy.xml";);
FileOutputStream out=new FileOutputStream("C:\\Apps\\my folder\\sdn\\copy1.xml";);
genFormat.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
public void execute(InputStream instream, OutputStream out)
throws StreamTransformationException {
// String for constructing target message structure
String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:newapi-javamapping\">");
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(instream);
traversingXML(doc);
}
catch(Exception e){}
fresult = fresult.concat(result);
fresult = fresult.concat("</ns0:MT_Customer>");
try{
out.write(fresult.getBytes());
/* assigning the created target message to "TransformationOutput"*/
}
catch(IOException e1){}
}
/*DOM Parser */
public void traversingXML(Node node)
{
NodeList children = node.getChildNodes();
for(int i=0;i<children.getLength();i++)
{
Node child = children.item(i);
short childType = child.getNodeType();
if(childType==Node.ELEMENT_NODE)
{
String nodeName=child.getNodeName();
String targetNodeName=null;
if(nodeName.equals("Users"))
targetNodeName="Customers";
else if(nodeName.equals("ID"))
targetNodeName="CustomerID";
else if(nodeName.equals("UserName"))
targetNodeName="Name";
else if(nodeName.equals("City"))
targetNodeName="City";
else if(nodeName.equals("State"))
targetNodeName="State";
else if(nodeName.equals("Country"))
targetNodeName="Country";
if(targetNodeName!=null)
result=result.concat("<"+targetNodeName+">");
traversingXML(child);
if(targetNodeName!=null)
result=result.concat("</"+targetNodeName+">");
}
else if(childType==Node.TEXT_NODE)
{
String nodeValue = child.getNodeValue();
result = result.concat(nodeValue);
}
}
}
}
Please put the input and output xml files in locations specified in main() method.
You alter the path as per your convinience.
Hope this solves your problem.
Regards
Anupam
N.B - Some parts of the code is not being displayed properly in forum. Please copy paste the code for the function traversingXML from the original article itself.
Edited by: anupamsap on Jan 25, 2012 9:09 AM
I have tested this and it is working..please chk the same
package com.test;
import java.io.IOException;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
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 JavaMapping extends AbstractTransformation {
/* The transform method which must be implemented */
public void transform(TransformationInput in,TransformationOutput out) throws StreamTransformationException
{
this.execute(in.getInputPayload().getInputStream(),
out.getOutputPayload().getOutputStream());
}
String result="";
public void execute(InputStream in1, OutputStream out1)
throws StreamTransformationException {
// String for constructing target message structure
String fresult="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
fresult = fresult.concat("<ns0:MT_Customer xmlns:ns0=\"urn:newapi-javamapping\">");
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(in1);
traversingXML(doc);
}
catch(Exception e){}
fresult = fresult.concat(result);
fresult = fresult.concat("</ns0:MT_Customer>");
try{
out1.write(fresult.getBytes());
/* assigning the created target message to "TransformationOutput"*/
}
catch(IOException e1){}
}
/*DOM Parser */
public void traversingXML(Node node)
{
NodeList children = node.getChildNodes();
for(int i=0;i<children.getLength();i++)
{
Node child = children.item(i);
short childType = child.getNodeType();
if(childType==Node.ELEMENT_NODE)
{
String nodeName=child.getNodeName();
String targetNodeName=null;
if(nodeName.equals("Users"))
targetNodeName="Customers";
else if(nodeName.equals("ID"))
targetNodeName="CustomerID";
else if(nodeName.equals("UserName"))
targetNodeName="Name";
else if(nodeName.equals("City"))
targetNodeName="City";
else if(nodeName.equals("State"))
targetNodeName="State";
else if(nodeName.equals("Country"))
targetNodeName="Country";
if(targetNodeName!=null)
result=result.concat("<"+targetNodeName+">");
traversingXML(child);
if(targetNodeName!=null)
{
result=result.concat("</"+targetNodeName+">");
} }
else if(childType==Node.TEXT_NODE)
{
String nodeValue = child.getNodeValue();
result = result.concat(nodeValue);
}
}
}
public static void main(String[] args) {
try{
JavaMapping javaMapping =new JavaMapping();
FileInputStream in=new FileInputStream("D:\\Input.xml");
FileOutputStream out=new FileOutputStream("D:\\output.xml");
javaMapping.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
AM
Thanks guys. Much appreciated, this will be very useful for learning.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
78 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.