SAX is a abbreviation and means "simple API for XML". A Java SAX XML parser is a stream oriented XML parser. It works by iterating through the XML and call certain methods on a "listener" object when it meets certain structural Elemen TS of the XML. For instance, it would call the listener object for the following "events":
- startdocument- startelement- characters- comments- processing instructions - endElement-enddocument
This list was probably not complete, but it was long enough to give what's it works. Let's move on to see how do you create and use a Java SAX Parser.
SAXParserFactory factory = saxparserfactory.newinstance (); Try { inputstream xmlinput new fileinputstream ("Thefile.xml"); SAXParser = factory.newsaxparser (); DefaultHandler handler new Saxhandler (); Catch (Throwable err) { err.printstacktrace ();}
When to call the saxparser.parse ()
method The SAX parser starts the XML processing. the xmlinput
inputstream
passed as parameter to the Parse () The
method is where the XML was read from. Notice the saxhandler
instance being created, and passed as parameter to the Parse () /code> method. the saxhandler
class is a subclass of the class Org.xml.sax.helpers.DefaultHandler
. the DefaultHandler
class comes with the JDK.
While processing the XML " SAXParser
the calls methods DefaultHandler
in the subclass" here, the "instance corresponding to" what SaxHandler
th E parser finds in the XML file. To react to those method calls your override the corresponding methods in the DefaultHandler
subclass. Here are an example:
Public classSaxhandlerextendsDefaultHandler { Public voidStartdocument ()throwssaxexception {} Public voidEnddocument ()throwssaxexception {} Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {} Public voidendElement (String uri, String localname, string qName)throwssaxexception {} Public voidCharacters (CharCh[],intStartintlength)throwssaxexception {} Public voidIgnorablewhitespace (CharCh[],intStartintlength)throwssaxexception {}}
It is the responsibility of the DefaultHandler
subclass to extract any necessary information from the XML via these methods. If you need to build a object graph based on a XML file, you'll have a to build that object graph inside the DefaultHandler
SUBCL The.
ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJavax.xml.parsers.SAXParser;Importjavax.xml.parsers.SAXParserFactory;Importorg.xml.sax.Attributes;Importorg.xml.sax.SAXException;ImportOrg.xml.sax.helpers.DefaultHandler; Public classTestsaxparser { Public Static voidTest () {SAXParserFactory factory=saxparserfactory.newinstance (); Try{InputStream Xmlinput=NewFileInputStream ("Thefile.xml"); SAXParser SAXParser=Factory.newsaxparser (); DefaultHandler Handler=NewSaxhandler (); Saxparser.parse (Xmlinput, handler); } Catch(Throwable err) {err.printstacktrace (); } } Public Static voidMain (string[] args) {testsaxparser.test (); }}classSaxhandlerextendsDefaultHandler { Public voidStartdocument ()throwssaxexception {} Public voidEnddocument ()throwssaxexception {} Public voidstartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {System.out.println (URI+ ":" + LocalName + ":" +qName); } Public voidendElement (String uri, String localname, string qName)throwssaxexception {System.out.println (URI+ ":" + LocalName + ":" +qName); } Public voidCharacters (CharCh[],intStartintlength)throwssaxexception {} Public voidIgnorablewhitespace (CharCh[],intStartintlength)throwssaxexception {}}
Java SAX Parser