SAX is mainly parsed in a serial and hierarchical manner. The following is a simple parsing example:
1. Select an xml document to be parsed.
<? Xml version = "1.0"?> <Br/> <! DOCTYPE book SYSTEM "DTD/JavaXML. dtd"> <br/> <! -- Java and XML Contents --> <br/> <book xmlns = "http://www.oreilly.com/javaxml2" xmlns: ora = "http://www.oreilly.com" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.oreilly.com/javaxml2 XSD/contents. xsd <br/> http://www.oreilly.com XSD/contents-ora.xsd "> <br/> <title ora: series =" Java "> Java and XML </title> <br/> <! -- Chapter List --> <br/> <contents> <br/> <chapter title = "Introduction" number = "1"> <br/> <topic name = "XML Matters "/> <br/> <topic name =" What's Important "/> <br/> <topic name =" The essenessen"/> <br/> <topic name = "What's Next? "/> <Br/> </chapter> <br/> <chapter title =" Nuts and Bolts "number =" 2 "> <br/> <topic name =" basics "/> <br/> <topic name =" Constraints "/> <br/> <topic name =" Transformations "/> <br/> <topic name =" And more... "/> <br/> <topic name =" What's Next? "/> <Br/> </chapter> <br/> <chapter title =" SAX "number =" 3 "> <br/> <topic name =" Getting Prepared" /> <br/> <topic name = "SAX Readers"/> <br/> <topic name = "Content Handlers"/> <br/> <topic name = "Gotcha! "/> <Br/> <topic name =" What's Next? "/> <Br/> </chapter> <br/> </contents> <br/> </book> <br/>
2. Use SAX for parsing. The Code is as follows:
Package javaxml2; </p> <p> import java. io. IOException; <br/> import java. io. outputStreamWriter; </p> <p> import org. xml. sax. attributes; <br/> import org. xml. sax. contentHandler; <br/> import org. xml. sax. inputSource; <br/> import org. xml. sax. locator; <br/> import org. xml. sax. SAXException; <br/> import org. xml. sax. XMLReader; <br/> import org. xml. sax. helpers. defaultHandler; <br/> import org. xml. sax. helpers. XMLReaderFactory; </p> <p> public class MySaxXml extends DefaultHandler {</p> <p>/** <br/> * @ param args <br/> * @ throws SAXException <br/> * @ throws Exception <br/> */<br/> private Locator locator; <br/> private int index1 = 0; <br/> private int index2 = 0; </p> <p> public void characters (char [] ch, int start, int length) throws SAXException {<br/> // TODO automatically generate method stubs <br/> // System. out. print ("SAX Event: CHARACTERS ["); </p> <p> System. out. print ("book <"); <br/> try {<br/> OutputStreamWriter outw = new OutputStreamWriter (System. out); <br/> outw. write (ch, start, length); <br/> outw. flush (); <br/>}catch (Exception e) {<br/> e. printStackTrace (); <br/>}</p> <p> System. out. println ("> directory"); <br/> // System. out. println ("]"); <br/>}</p> <p> public void endDocument () throws SAXException {<br/> // TODO automatically generates method stubs <br/> System. out. println ("Resolution ended:"); <br/>}</p> <p> public void endElement (String uri, String localName, String qName) throws SAXException {<br/> // TODO automatically generates method stubs <br/> // System. out. println ("SAX Event: end element [" + localName + "]"); <br/> // this. index2 = 0; <br/> if (qName. equalsIgnoreCase ("chapter") <br/> this. index2 = 0; <br/>}< br/> public void setDocumentLocator (Locator locator) {<br/> // TODO automatically generates method stubs <br/> this. locator = locator; <br/>}< br/> public void startDocument () throws SAXException {<br/> // TODO automatically generates method stubs <br/> System. out. println ("Resolution start:"); <br/>}< br/> public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {<br/> // TODO automatically generates method stubs </p> <p> // System. out. println ("SAX Event: start element [" + localName + "]"); <br/> if (qName. equalsIgnoreCase ("chapter") {<br/> index1 ++; <br/> for (int I = 0; I <atts. getLength (); I ++) {<br/> // System. out. println (atts. getLength (); <br/> String attName = atts. getQName (I); <br/> if (attName. specified signorecase ("title") {<br/> System. out. println ("chapter" + index1 + ":" + atts. getValue (I); <br/>}</p> <p> if (qName. equalsIgnoreCase ("topic") {<br/> index2 ++; <br/> for (int I = 0; I <atts. getLength (); I ++) {<br/> String attName = atts. getQName (I); <br/> if (attName. specified signorecase ("name") {<br/> System. out. println ("section" + index2 + "section:" + atts. getValue (I); <br/>}< br/> public static void main (String [] args) throws Exception {<br/> // TODO automatically generates method stubs </p> <p> String vendorParserClass = "org. apache. xerces. parsers. SAXParser "; <br/> String xmlURI =" http: // localhost: 8080/TestXml/contents. xml "; <br/> XMLReader reader = <br/> XMLReaderFactory. createXMLReader (vendorParserClass); <br/> reader. setContentHandler (new MySaxXml (); <br/> InputSource inputSource = new InputSource (xmlURI); <br/> reader. parse (inputSource); <br/>}< br/>}
3. contentHandler only implements several of its methods. The parsing result is as follows:
Resolution start: <br/> the <Java and XML> directory of the book <br/> Chapter 1: Introduction <br/> Part 3: XML Matters <br/> Part 1: What's Important <br/> Part 2: The Essentials <br/> Part 3: What's Next? <Br/> Chapter 2: Nuts and Bolts <br/> Part 1: The Basics <br/> Part 2: Constraints <br/> Part 3: transformations <br/> Part 1: And More... <br/> Part 1: What's Next? <Br/> Chapter 2: SAX <br/> Part 2: Getting Prepared <br/> Part 3: SAX Readers <br/> Part 3: content Handlers <br/> Part 1: Gotcha! <Br/> Part 1: What's Next? <Br/> resolution completed <br/>
After reading: I used this example when I was just learning about parsing. In fact, it is so simple to parse SAX, and it is not difficult to implement it by myself. xml is so rule-oriented. Now, java's various framework technologies are designed for decoupling, xml is inseparable from parsing.