SAX Parser is now a days people are using for most of RSS feed reading and enhancing their websites with latest updates . Most of the contenet management developers are required xml reading and parsing. But here SAX parse is root parse to all the XML parsing techniques . But SAX parser is top down approach and it will not come back to the root node . I dont wanna boring you guys by giving more lecture on SAX parse and its mechanism .I just want to make it simpler which I understood . SAX requires InputStream as source to DocumentHandler . I have developed one code which is pertaining to the JAXB mechanism . Using SAX Parser . first of all lemme tell you about JAXB working scenario . JAXB is pure DOM model in which it has Marshelling and Unmarshelling techniques . Marshelling means converting all xml nodes to JAVA objects in the same way unmarshelling is a technique in reverse manner . Here is a sample snippet code I have written based on my knowledge . Please find code below and let me know the suggestions and comments .But dont use same code use your own logic to implement JAX using SAX
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package saxparser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.RandomAccess;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;
/**
*
* @author venkat
*/
public class Main extends HandlerBase {
private OutputStreamWriter out;
private static Root r[]=new Root[100];
static int count=0;
boolean nameF=false,salaryF=false;
String temp="";
public Main(){
for(int i=0;i<99;i++)
{
r[i]=new Root();
}
try{
SAXParserFactory factory= SAXParserFactory.newInstance();
try {
//try {
//try {
out = new OutputStreamWriter(System.out, "UTF-8");
// } catch (UnsupportedEncodingException ex) {
/// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//}
SAXParser saxParser;
// try {
saxParser = factory.newSAXParser();
saxParser.parse(new File("/home/venkat/NetBeansProjects/SaxParser/src/saxparser/employee.xml"), this);
//} catch (ParserConfigurationException ex) {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
///} catch (SAXException ex) {
//Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//}
// } catch (UnsupportedEncodingException ex) {
//Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
//} catch (ParserConfigurationException ex) {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
///} catch (SAXException ex) {
//Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
//}
// } catch (UnsupportedEncodingException ex) {
//Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Main m=new Main();
System.out.println(m.temp);
StringTokenizer st=new StringTokenizer(m.temp,"+");
while(st.hasMoreTokens())
{
String stt=st.nextToken();
StringTokenizer sttt=new StringTokenizer(stt,"~");
while(sttt.hasMoreTokens())
{
String me=sttt.nextToken();
if(me.trim().length()!=0){
if(me.contains("."))
me=me.substring(0,me.length()-1);
System.out.println(me);
}
}
System.out.println();
}
}
@Override
public void startDocument() throws SAXException {
System.out.println("START DOCUMENT");
}
@Override
public void endDocument() throws SAXException {
System.out.println("END DOCUMENT");
}
@Override
public void startElement(String name, AttributeList attributes) throws SAXException {
System.out.println("START ELEMENT:---->"+name);
temp=temp+name+".";
try {
// if(name.trim().equalsIgnoreCase("employee"))
// {
// // r[count]=new Root();
// }
// if(name.trim().equalsIgnoreCase("employee"))
// {
// r[count].setEmployee(new Employee());
// }
// if(name.trim().equalsIgnoreCase("name"))
// {
// nameF=true;
// }
// if(name.trim().equalsIgnoreCase("salary"))
// {
// salaryF=true;
// }
if(!name.equalsIgnoreCase("root"))
name=name+"_"+count;
FileOutputStream f = new FileOutputStream("./"+name+".java");
RandomAccessFile raf=new RandomAccessFile(new File("./"+name+".java"), "rw");
try {
raf.writeUTF("public class " + name + " { }");
//File ff=new File("./"+name);
//ff.mkdir();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
//File ff=new File("./"+name);
//ff.mkdir();
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void endElement(String name) throws SAXException {
System.out.println("END ELEMENT");
// if(name.trim().equalsIgnoreCase("employee"))
// {
// count++;
// }
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String t=new String(ch,start,length);
//System.out.println("S1:---->"+s);
if(t!=null && t.trim().length()!=0)
temp=temp+"~"+new String(ch,start,length)+"~+";
//System.out.println("S2:---->"+s);
// if(nameF)
// {
// System.out.println(nameF+":"+new String(ch,start,length));
// try{
// r[count].getEmployee().setName(new String(ch,start,length));
// }catch(Exception e){
// r[count].setEmployee(new Employee());
// r[count].getEmployee().setName(new String(ch,start,length));
// }
// }
// if(salaryF)
// {
// System.out.println(nameF+":"+new String(ch,start,length));
// r[count].getEmployee().setSalary(new String(ch,start,length));
//
// }
System.out.print("CHARACTER METHOD:---->"+new String(ch,start,length));
}
}
xml
----
A
10000
A
10000
B
20000
C
30000
D
410000
|
Comments