Skip to Content
0
Former Member
Dec 13, 2006 at 11:31 AM

Java Mapping

27 Views

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