How to use JSP to develop SAX applications? SAX is an abbreviation of Simple API for XML. It is not a standard proposed by W3C, which can be said to be "folk" factual standard. In fact, it is a discussion product of community nature. Nonetheless, the application of SAX is less than the DOM in XML, and almost all XML parsers will support it.
Compared with DOM, SAX is a lightweight method. We know that when processing DOM, we need to read the entire XML document, then create a DOM tree in memory, generate each Node object on the DOM tree. When the document is small, this will not cause any problem, but once the document is large, the DOM will become quite cost-effective. In particular, its demand for memory will also be multiplied, so that in some applications, using DOM is a very disadvantage (such as in applet). At this time, a better alternative solution is SAX.
SAX is conceptually different from DOM. First, different from the DOM document driver, it is an event-driven, that is, it does not need to read the entire document, and the read process of the document is the resolution process of SAX. The so-called event drive refers to a program operation method based on a callback mechanism. (If you are more clear about the Java new agent event model, it is easy to understand this mechanism) In the XMLReader accepts XML documentation, parsing in the process of reading the XML document, that is, the process of reading documents The process of parsing is carried out simultaneously, this is very much different from the DOM. Before the analysis begins, you need to register a contentHandler to XmlReader, which is equivalent to an event listener, defining a lot of methods in ContentHandler, such as startDocument (), which customizes when in the parsing process, should be processed at the beginning of the document thing. When XmlReader reads the right content, the corresponding event will be thrown, and the processing of this event is given to ContentHandler, calling its corresponding method to respond.
This generally said that some are not easy to understand, don't worry, followed by the examples will let you understand the resolution process of SAX. Take a look at this simple XML file:
When XmlReader reads the
Project method callback
{Document start} StartDocument ()
Void Characters (char [] ch, int start, int future): This method is used to handle the character string in the XML file, and its parameters are a character array, as well as the character read from this array. Sitting position and length, we can easily use a String class String class: string Charencontered = New String (CH, Start, LENGTH).
Void StartDocument (): When you encounter the beginning of the document, call this method, you can do some pre-processed work.
Void EndDocument (): The method is corresponding to the method. When the document ends, call this method, you can do some kind of work.
Void StartElement (String Namespaceuri, String LocalName, String Qname, Attributes Atts): This method is triggered when you read a start tag. The name domain is not supported in the SAX1.0 version, and the support for the name domain is provided in the new version 2.0. The Namespaceuri in the parameters is the name domain. LocalName is the label name, QNAME is the tag's modified prefix, when not These two parameters are not null when using the name of the domain. And ATTS is a list of properties included in this tag. With ATTS, all attribute names and corresponding values can be obtained. It should be noted that an important feature in SAX is that its stream processing. When you encounter a label, it does not record the label you have encountered before, that is, in the startElement () method, all The information you know is the name and attribute of the label. As for the nested structure of the label, the name of the upper label, whether there is a sub-genus, etc., it is not known, you need you. The program is completed. This makes SAX no DOM in programming processing is so convenient. Void endelement (String Namespaceuri, String LocalName, String Qname): This method is corresponding to the above method. When you encounter an end tag, call this method.
We still follow the document examples used in DOM, but first, let's take a simple application, we hope to count the number of times of each label in the XML file. This example is simple, but it is enough to explain the basic ideas of SAX programming. Of course, the Import statement is of course the IMPORT statement:
Import org.xml.sax.helpers.defaulthandler; import javax.xml.parsers. *; import org.xml.sax. *; import org.xml.sax.helpers. *; import java.util. *; import java. IO. *;
Then we create a class inherited in the defaulthandler, the specific program logic can be temporarily in the same side, pay attention to the structure of the program:
public class SAXCounter extends DefaultHandler {private Hashtable tags; // this Hashtable used to record the number of times the tag appears before processing document // public void startDocument () throws SAXException {tags = new Hashtable (); //} // initialize Hashtable Processing each of the starting elements (String Namespaceuri, String Localname, String Rawname, Attributes atts) throws saxception {string key = localname; ......
Let's take a look at what this program is made. In the main () method, the main thing is to create a parser and parse the document. In fact, when you create a SAXPARSER object here, in order to make the program code on the specific parser, use the same design skills as DOM: Create a concrete SAXPARSER object through a SAXPARSERFActory class, so that different uses When the parser is changed, it is only a value of an environment variable, and the code's code can remain unchanged. This is the idea of FactoryMethod mode. It is no longer specifically here. If you still don't understand, you can see the explanation in the DOM above, the principle is the same. However, there is still a point here to pay attention to the relationship between the SAXPARSER class and the XMLReader class. You may be a bit confused. In fact, SAXPARSER is a package class in JAXP to XmlReader, and XMLReader is a interface to parse documents in SAX2.0. You can call the Parser () method in the XMLReader to resolve the document, the effect is exactly the same. However, the Parser () method in SAXPARS is accepted more parameters, which can be parsed for different XML document data sources, so it is convenient than XmlReader.
This example only involves a little fur of SAX, and this is to be advanced. Below we have to implement, in the example of the DOM, it is to read the content from the XML document and format the output, although the program logic looks very simple, but SAX is not more than DOM, look at it.
As mentioned earlier, when you encounter a start label, in the StarTelement () method, we don't get this label where you are in the XML document. This is a big trouble when dealing with the XML document, because in the semantics of the tag in XML, some is determined by the location of its location. And in some programs that need to verify the document structure, this is a problem. Of course, there is no problem that you can't solve, we can use a stack to implement a record of the document structure.
The stack is characterized by advanced first, our current idea is that in the startelemnt () method, use Push to add this tag's name to the stack, in the endelement () method, come out of POP. We know that the nesting structure is complete, and each start label will always correspond to an end tag, and there is no misplacement between label nesses. Thus, each STARTELEMENT () method will inevitably correspond to the call of the endelement () method, so that PUSH and POP are also paired, we only need to analyze the structure of the stack, you can easily know the current label In the location of the document structure.
Public class saxreader extends defaulthandler {java.util.stack tags = new java.util.stack (); ......
Although there is no analysis of the stack here, the actual stack is a very easy thing, and should inherit the java.util.vector class for java.util.stack, and the elements in Stack are the structure of the stack. From the bottom to the top, because we can use the Size () method of the VECTOR class to get the number of elements of the Stack, and you can use the VECTOR's Get (int) method to get the specific permaster. In fact, if you are arranged one by one from the bottom, we get a unique path from the XML root node to the current node, with this path information, the structure of the document is clear. So far, we have mastered two major weapons programming for XML: DOM and SAX, and also know how to use them in a Java program. The DOM programming is relatively simple, but the speed is slower, and the memory is more memory, and S AX programming is more complicated, but the speed is fast, and the memory is less. Therefore, we should choose different methods according to different environments. Most XML applications can basically be solved by them. In particular, the DOM and SAX are actually language-independent, not Java unique, that is, as long as there is a corresponding language implementation, DOM and SAX can be applied in any object-oriented language.