XML Parser
An
XML parser is a piece of code that reads an XML document and analyzes its
structure. There are different kinds of XML parsers- SAX, DOM, and JAXP.
SAX:
The Simple API for XML
Overview
SAX is a push API.
You create a SAX parser, and the parser tells you (it pushes events to you)
when it finds things in the XML document. Specifically, here's how SAX
addresses the concerns mentioned in the previous panel:
SAX doesn't build an
in-memory tree of an XML document. A SAX parser sends you events as it finds
things in an XML document. It's up to you to decide how (or if) you want to
store that data.
A SAX parser doesn't create
any objects. You have the option of creating objects if you want, but that's
your decision, not the parsers.
SAX starts sending you events immediately. You
don't have to wait for the parser to finish reading the document.
SAX events
The SAX API defines a
number of events. Some commonly used SAX events are given below.In a SAX-based XML application, you'll spend
most of your time looking at five basic events.
startDocument()
This event tells you that the parser has found the start of the
document. This event doesn't pass you any information, it simply lets you know
that the parser is about to begin scanning the document.
endDocument ()
This event tells you that the parser has found the end of the
document.
startElement(...)
This event tells you that the parser has found a start tag. This
event tells you the name of the element, the names and values of all of the
element's attributes, and gives you some namespace information as well.
characters
(...)
This event tells you that the parser has found some text. You get
a character array, along with an offset into the array and a length variable;
together, those three variables give you the text the parser found.
endElement(...)
This event tells you that the parser has found an end tag. This
event tells you the name of the element, as well as the associated namespace
information.
startElement(),
characters(), and endElement() are the most important events. All of the events
here are part of the ContentHandler interface; other SAX interfaces are defined
to handle errors as well as entities and other rarely-used things.
SAX Parser Program
//Simple Sax Parser Program
//SAX.java
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
public class SAX
{
public static void main(String[] args)
{
try
{
File f=new
File("www.xml");
SAXParserFactory
spf=SAXParserFactory.newInstance();
SAXParser
sp=spf.newSAXParser();
mySAXHandler sh=new mySAXHandler();
sp.parse(f,sh);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class mySAXHandler extends
HandlerBase
{
public void
startDocument()
{
System.out.println("start
document");
}
public
void endDocument()
{
System.out.println("end
document");
}
public
void startElement(String name,AttributeList al)
{
System.out.println("start "+name+" Element");
}
public void
endElement(String name)
{
System.out.println("end
"+name+" Element");
}
}
//Input XML file which is supplied to Sax parser program for
parsing
//www.xml
<?xml
version="1.0" encoding="UTF-8"?>
<weather>
<day1>
<location>SanFransico</location>
<date>25/04/2013</date>
<temp>42'c</temp>
</day1>
</weather>
How to compile & Run this program
Ø
Save both the program in Jdk/bin
directory
Ø
Compile SAX.java program in
usual java programming way
Ø
javac SAX.java
Ø
run the same program
Ø
java SAX.java
You will get the Output.Your valuable comments are waiting .thank you...