on 12-13-2006 11:39 AM
Hi all,
Could anybody please give me sample code for java mapping ??
Thanks & Regards,
Nandini
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nandini,
Refer these links...
http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/content.htm
/people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs
/people/swaroopa.vishwanath/blog/2005/06/29/generic-approach-for-validating-incoming-flat-file-in-sap-xi--part-ii
Regards,
Uma
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ,
Check this...
package com.sap.pack;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class JavaMap extends DefaultHandler implements StreamTransformation {
public static void main(String args[]) {
try {
FileInputStream fi = new FileInputStream("D:/java.ip.xml");
FileOutputStream fo = new FileOutputStream("D:/op.xml");
JavaMap imap = new JavaMap();
imap.execute(fi,fo);
fi.close();
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private OutputStream arg1;
private Map map;
private int tag;
public void setParameter(Map arg0) {
map = arg0;
}
private void execute(FileInputStream fi, FileOutputStream fo) throws StreamTransformationException
{
DefaultHandler handler = this;
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp= spf.newSAXParser();
sp.parse(fi,handler);
}
public void startDocument() throws SAXException
{
write("<?xml version='1.0' encoding='UTF-8'?>");
System.out.println("<?xml version='1.0' encoding='UTF-8'?>");
}
private void write(String string) throws SAXException, IOException{
// TODO Auto-generated method stub
arg1.write(string.getBytes());
arg1.flush();
}
public void endDocument ()throws SAXException
{
arg1.flush();
}
public void startElement(String uri,String sname,String qname,Attributes attribute) throws SAXException
{
String name=sname;
if (name.equals(""))
{
name=qname;
}
else if(name.equals("<b>Src_MT</b>"))
{
tag=5;
write("<ns:Dest_MT xmlns:ns="+""http:\JavaMapping_test""+">");
}
else if(name.equals("Emp_Name"))
{
tag=1;
}
else if(name.equals("Emp_No"))
{
tag=2;
}
else tag=3;
}
public void endElement(String uri,String sname,String qname) throws SAXException
{
String name=sname;
if (name.equals(""))
{
name=qname;
}
else if(name.equals("Emp_Name"))
{
tag=1;
write("</Ename>");
}
else if(name.equals("Emp_No"))
{
tag=2;
write("</Eno>");
}
else if(name.equals("<b>Src_MT</b>"))
{
tag=5;
write("<ns:Dest_MT xmlns:ns="+""http:\JavaMapping_test""+">");
}
}
public void characters(char[]ch,int start,int length) throws SAXException
{
String s=new String(ch,start,length);
System.out.println("characters:"+ s);
switch(tag)
{
case 1:
write(s);
case 2:
write(s);
System.out.println("char :"+ s);
case 3:
write(s);
System.out.println("char :"+ s);
}
}
}
Hi,
The execute statement signature is not correct.
It should be
public void execute(InputStream fi, OutputStream fo) throws StreamTransformationException
and also import statements are missing like...
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
Exceptions are not handled for many statements using SAX parser.
Check the code using any IDE (NWDS or Eclipse). It will easy to find errors.
Regards,
Uma
hi Nandini,
please check
/people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i
/people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii
/people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-iii
Hope this help
Francesco
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.