SAX refers to Sample Application for XML. His work process can be explained in such an example. There is a number of positive numbers, and the number of positive and negative numbers should be statistically, and the method we use is to start a one and zero comparison, and then calculate the corresponding number. This method is similar to the SAX method. The SAX handling XML method is also the case, and the read file we have learned from the beginning of the reading file is processed. The application of SAX in Java will be described below.
To use SAX to process XML in Java, you must first have an XML parsing class, but also have a SAX class and the corresponding XML document. XML parses can use apache Xerces or Xalan et al, use Xerces in the following examples (you can search with Google). The processing of XML can be divided into two parts, first of all, start reading the XML file through the XMLReader, and then the processing of the XML file can then be completed by registering the corresponding processing class. The registration class mainly includes: content handle, error handling class, entity processing class (EntityResolver), DTD processing class (DTDHandler). The corresponding process can be completed by registration of these classes. Use the corresponding SET method to complete the registration of the class of the corresponding to XMLReader. Here is the method of using the content class.
The content processing class is in the pair, including the processing method of various capable elements in the contents of the XML, each element corresponds to a START method and an end method, through the implementation of the implementation of the two methods. . It can be considered that the processing of XML is complete through his processing class. The simple example of generating JTREE through an XML file is given by the "Java and XML" book.
Package testsax;
Import javax.swing. *;
Import java.awt. *;
Import org.xml.sax. *;
Import java.io.ioException;
Import javax.swing.tree.defaultmutabletreenode;
Import javax.swing.tree.defaulttreemodel;
Public class saXTree Extends jframe {
Private org.xml.sax.xmlreader Reader;
Private Jtree Tree;
Public saXTree () {
INIT ();
This.setsize (800, 600);
THIS.SETDEFAULTCLOSEOPERATION (JFrame.exit_ON_CLOSE);
THIS.SHOW ();
}
Public void init ()
{
Try {
Defaultmutabletreenode root = new defaultmutabletreenode ("root");
DEFAULTTREEMODEL MODEL = New DefaultTreeModel (root);
Tree = new jtree (model);
InputSource IN = New InputSource ("first1.xml");
Reader = Org.xml.sax.helpers.xmlreaderFactory.createxmlreader ("Org.Apache.xerces.Parsrs.saxparser");
Reader.SetContentHandler (New Content (root);
Reader.Parse (IN); Model.Reload (root);
THIS.GETCONTENTPANE (). add (new jscrollpane (tre), borderlayout.center;
}
Catch (SAXEXCEPTION EX) {
EX.PrintStackTrace (System.out);
}
Catch (IOException EX) {
EX.PrintStackTrace (System.out);
}
}
Public static void main (String [] args) {
SaxTree Tree = new saXTree ();
}
}
Class Content IMPLEments ContentHandler
{
Defaultmutabletreenode Node;
DefaultMutableTreenode Current;
Public Content (DefaultmutableTreenode Node)
{
THIS.NODE = Node;
THIS.CURRENT = NODE;
}
Public void enddocument () {
Current = (defaultmutabletreenode) current.getparent ();
}
Public void startdocument () {
Defaultmutabletreenode Doc = New DefaultmutableTreenode ("Document");
Current.Add (DOC);
Current = DOC;
}
Public void characters (char [] ch, int start, innet) {
String Data = String.copyValueof (ch, start, length);
IF (Data! = null && Data.trim (). Length ()! = 0)
{
DefaultmutableTreenode Temp = New DefaultMutableTreenode (data);
Current.add (TEMP);
// system.out.println ("DATA IS: DATA);
}
}
Public void ignorablewhitespace (char [] ch, int start, int layth) {
}
Public void endprefixmapping (string prefix) {
THIS.CURRENT = (defaultmutabletreenode) current.getparent ();
}
Public void Skippedentity (String Name) {
}
Public void setDocumentlocator (locator location) {
}
Public void processinginstruction (String Target, String Data) {
}
Public void startprefixmapping (string prefix, string URI) {
Defaultmutabletreenode Pref = New DefaultmutableTreenode (Prefix ": URI);
THIS.CURRENT.ADD (pref);
THIS.CURRENT = pref;
System.out.println (Prefix ":" URI);
}
Public void endelement (String Namespaceuri, String Localname, String Qname) {current = (defaultmutabletreenode) current.getParent ();
}
Public void StartElement (String Namespaceuri, String LocalName, String Qname,
Attributes atts) {
INT length = atts.getlength ();
String attributes = "";
DefaultmutableTreenode Temp;
For (INT i = 0; I { Attributes = attributes atts.getqname (i) ": atts.getvalue (i) " } IF (attributes.length ()> 0) { Temp = New DefaultMutableTreenode ("Element:" Qname "Attibutes:" attributes); } Else { Temp = New DefaultmutableTreenode ("Element:" QNAME); } Current.add (TEMP); Current = TEMP; } } The XML file used in the example is as follows: First1.xml XML Version = "1.0" encoding = "UTF-8"?> student> student> student> student> SCHOOL> Through the above example we see that the SAX processing XML file is like processing an argument, sequentially processed from the header, and is not suitable for modification of this processing. The DOM described in the next chapter can overcome this shortcomings to complete the modification of the XML file.