/ * Since the true formation of XML (I think it is Org.W3C organization to release XML standard), XML has been developed very quickly, many manufacturers have their own XML parsers, such as apache's Xalan, IBM Xerces, Sun's JDOM, but these are JAXP-based JAXP (JAVA API for XML Process), from JDK 1.4.0, JAXP is attached to JAXP, which has brought great convenience to developers, which makes We no longer need to use third-party XML processors when dealing with the general XML function. With the rapid development of XML, SAX has also been 2.0 (still compatible with 1.0) from 1.0 to now (still compatible with 1.0), structure Have some big changes.
DOM (Document Object Model) Every time you read the XML node, you have to come to memory. When the document is large, it looks very slow, SAX (Simple API for XML) is an interface of an XML parser. It is lower than the DOM, it is an XML processing based on event and callback mode, so DOM is not better than the resolution speed (even when the XML document to be parsed). So What is Event Response (Event) in SAX? I personally think this is a bit similar to Swing, AWT, and it refers to the processing that triggers some specific behavior, such as: mouse Click events, etc. Here, it refers to the processing that encounters a specific XML node, such as the startDocument, the enddocument, the beginning of the element (startelement), let's take a look at the method in the API of SAX The name knows what the event is, basically can be used to see the literacy. Only want to analyze XML content (read-only), require high performance, flexibility to locate the error message (SAX is able to locate the wrong line position) It is best to do with SAX. In general, SAX is used in the following principle: <1> Setting the event processor (SAX 1.0 is set to use a instance of the HandlerBase class, SAX 2.0 is inheriting DefaultHandler And useful in the XMLReader mode, there is no big difference in principle) <2> Load to the content to be resolved <3> In the event method that needs to be parsed (specifically see the SAX API document) Add its own control logic. < 4> Repeat <3> until the parsing is complete. Here I wrote an XML file (file.xml) describing the movie poster information, written a very simple XML content reader with SAX2.0 to resolve it, and Everyone exchanges his experience. The program has been tested in my machine (OS: Win2k Advanced Server (English Version), Intel Pentium CPU, 256M RAM) * /
Import org.w3c.dom. *; import org.xml.sax. *; import org.xml.sax.helpers. *; import javax.xml.parsers. *;
Class myxmlreader extends defaulthandler {// fields private int index; private locator locator;
// constructor public myXmlreader () {super (); // it must be done!}
// nain method public static void main (String [] args) {try {SAXParserFactory sf = SAXParserFactory.newInstance (); SAXParser sp = sf.newSAXParser (); MyXMLReader reader = new MyXMLReader (); sp.parse (new InputSource ( "Film.xml"), reader;} catch (exception e) {E.PrintStackTrace ();}} // Response the startdocument Event public void startdocument () {system.out.println ("/ n **** **************************** (: New Year's Movie Poster :) ************* ******************** / N ");}
// Response the startElement event public void startElement (String uri, String localName, String qName, Attributes attrs) {if (qName.equalsIgnoreCase ( "film")) {index ; int attrCount = attrs.getLength (); for ( INT i = 0; i
// response the enddocument ("/ t / t / t / t / t / t / t / t / t / t / t / t / t / t / t / t / t / t / t ------------------------------------------------------------------------------------------------------------------------------------------------------ }
// Response the endElement event public void endElement (String uri, String localName, String qName) {// add your codes if neccessary ...} // Print the fata error information public void fatalError (SAXParseException e) {System.out. Println ("/ NFATAL ERROR INFORMATION ->"); system.out.println ("/ t" E.getMessage ()); system.out.println ("/ tat line" locator.getLineNumber () " , Column " Locator.getColumnNumber ());} // Print The Usual Error Information Public Void Error (SAXPARSEEXCEPTION E) {System.out.Println (" / Nusual Error Information -> "); System.out.Println "/ t" E.getMessage ()); System.out.Println ("/ Tat line" locator.getLinenumber () ", column" locator.getColumnNumber ());} // print the warning information public Void Warning (SAXPARSEEXCEPTION E) {system.out.println ("/ nwarning information ->"); system.out.println ("/ t" E.getMessage ()); system.out.println ("/ tat Line " Locator.getLinenumber () ", Column " Locator.getColumnNumber ());
// store the error locator Object public void setDocumentlocator (local LCT) {locator = LCT;
} // end class myxmlreader
Attached: Film.xml complete content: