on 12-13-2006 11:31 AM
Hi all,
I am just trying to do java mapping like simple 1 to 1 mapping.I am getting null pointer exception while running the application at execute and parse methods.
Could anybody please verify my code and let me know If I did any wrong codding.
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);
}
}
}
Your suggestions are valuable for me to solve this problem
Hi,
There are some standards to be followed for Java Mapping.
The Java Mapping class should implement StreamTransformation class and execute method.
public class JavaMapping implements StreamTransformation {
public void setParameter(Map map) {
}
public void execute(InputStream in, OutputStream out) throws StreamTransformationException {
// Code Mapping logic here
}
}
Modify your mapping and code all the logic inside execute method.
Also, If you any XML parsers like DOM or SAX, those jar files has to be imported in Repository.
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 Nandini,
Check my blog for correct using the API:
/people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio
When you post Java code, use the button "code".
Regards
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nandini,
Can you please align the code...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
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.