SAX(Simple API For XML) parser
Hello Friends .Today i am going to demonstrate you how to parse xml data using SAX(Simple API for XML) parser. The Basic Differance between sax & dom parser is dom parser loads entire document into memory where as sax parser do not load entire document into memory.so it is convinient way for parsing large xml data.
SAX parser is Event based parser.it does not create representation of xml tree.it simply parse xml data based on Events.we need to write handler class inorder to keep track of event such as starting of element,ending of element & when character is encountered.
SAX uses some callback methods to parse and read the xml accordingly. It uses three callback methods listed below :
(1)public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
(2)public void characters(char[] ch, int start, int length)
throws SAXException
(3) public void endElement(String uri, String localName, String qName)
throws SAXException
The Sample code is as given below:
package org.saxdemo;
public class Student
{
private String id;
private String name;
private String stream;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
}
//Handler class
package org.saxdemo;
import java.util.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Handler extends DefaultHandler
{
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudList() {
return studList;
}
public void setStudList(List<Student> studList) {
this.studList = studList;
}
private List<Student> studList;
private boolean bname;
private boolean bstream;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if(qName.equals("student"))
{
String id=attributes.getValue("id");
student=new Student();
student.setId(id);
if(studList==null)
{
studList=new ArrayList<>();
}
}
else if(qName.equalsIgnoreCase("name"))
{
bname=true;
}
else if(qName.equalsIgnoreCase("stream"))
{
bstream=true;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if(qName.equalsIgnoreCase("employee"))
{
studList.add(student);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
if(bname)
{
student.setName(new String(ch,start,length));
}
if(bstream)
{
student.setStream(new String(ch,start,length));
}
}
}
Sax parser Implementation :
package org.saxdemo;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.*;
public class XmlParser
{
public static void main(String args[])
{
//configure sax based parser
SAXParserFactory saxparserfactory=SAXParserFactory.newInstance();
try
{
//object object for sax based parser
SAXParser saxparser=saxparserfactory.newSAXParser();
Handler handler=new Handler();
//parsing of xml data
saxparser.parse(new File("D:\\students.xml"), handler);
List<Student> studlist=handler.getStudList();
for(Student s:studlist)
{
System.out.print(s);
}
}
catch(ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
}
}
i hope you got my point
for any query & improvement ping me on pathak.nisarg@yahoo.com
Hello Friends .Today i am going to demonstrate you how to parse xml data using SAX(Simple API for XML) parser. The Basic Differance between sax & dom parser is dom parser loads entire document into memory where as sax parser do not load entire document into memory.so it is convinient way for parsing large xml data.
SAX parser is Event based parser.it does not create representation of xml tree.it simply parse xml data based on Events.we need to write handler class inorder to keep track of event such as starting of element,ending of element & when character is encountered.
SAX uses some callback methods to parse and read the xml accordingly. It uses three callback methods listed below :
(1)public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
(2)public void characters(char[] ch, int start, int length)
throws SAXException
(3) public void endElement(String uri, String localName, String qName)
throws SAXException
The Sample code is as given below:
package org.saxdemo;
public class Student
{
private String id;
private String name;
private String stream;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
}
//Handler class
package org.saxdemo;
import java.util.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Handler extends DefaultHandler
{
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudList() {
return studList;
}
public void setStudList(List<Student> studList) {
this.studList = studList;
}
private List<Student> studList;
private boolean bname;
private boolean bstream;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if(qName.equals("student"))
{
String id=attributes.getValue("id");
student=new Student();
student.setId(id);
if(studList==null)
{
studList=new ArrayList<>();
}
}
else if(qName.equalsIgnoreCase("name"))
{
bname=true;
}
else if(qName.equalsIgnoreCase("stream"))
{
bstream=true;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if(qName.equalsIgnoreCase("employee"))
{
studList.add(student);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
if(bname)
{
student.setName(new String(ch,start,length));
}
if(bstream)
{
student.setStream(new String(ch,start,length));
}
}
}
Sax parser Implementation :
package org.saxdemo;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.*;
public class XmlParser
{
public static void main(String args[])
{
//configure sax based parser
SAXParserFactory saxparserfactory=SAXParserFactory.newInstance();
try
{
//object object for sax based parser
SAXParser saxparser=saxparserfactory.newSAXParser();
Handler handler=new Handler();
//parsing of xml data
saxparser.parse(new File("D:\\students.xml"), handler);
List<Student> studlist=handler.getStudList();
for(Student s:studlist)
{
System.out.print(s);
}
}
catch(ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
}
}
i hope you got my point
for any query & improvement ping me on pathak.nisarg@yahoo.com
No comments:
Post a Comment