XML-Parse XML using Sax

Source: Internet
Author: User

Simple APIs for XML, That is, simple XML application interface. Unlike Dom, the access mode provided by Sax is a sequential mode, which allows you to quickly read and write XML data. When you use the sax analyzer to analyze XML documents, a series of events are triggered and corresponding event processing functions are activated. Applications can use these event processing functions to access XML documents, therefore, the sax interface is also called an event-driven interface.
The sax analyzer triggers a series of events when analyzing XML documents. Because the event trigger itself has a time series, it provides a sequential access mechanism, for the analyzed part, you cannot reverse it and try again. The reason why Sax is called "simple" application interface is that the sax analyzer only does some simple work, and most of the work needs to be done by the application itself. That is to say, when the sax analyzer is implemented, it only checks the byte stream in the XML document in sequence to determine which part of the XML syntax is the current byte and whether it complies with the XML syntax, then, the corresponding event is triggered, and the event processing function itself must be implemented by the application itself. Compared with Dom analyzer, Sax analyzer lacks flexibility. However, due to the simple implementation of the sax analyzer and low memory requirements, the implementation efficiency is relatively high. For applications that only need to access the data in the XML document and do not change the document, A more suitable sax analyzer.

 

Example:

The steps to start using sax are consistent with the usage of Dom,

Saxparserfactory factory = saxparserfactory. newinstance ();
Saxparser parser = factory. newsaxparser ();

In the myhandler2 class of row 23rd, we can see three methods: startelement, characters, and endelement. These three methods are the implementation of the defaulthandler interface in JDK.

Package COM. sax; import Java. io. file; import Java. util. stack; import javax. XML. parsers. saxparser; import javax. XML. parsers. saxparserfactory; import Org. XML. sax. attributes; import Org. XML. sax. saxexception; import Org. XML. sax. helpers. defaulthandler; public class saxtest2 {public static void main (string [] ARGs) throws exception {saxparserfactory factory = saxparserfactory. newinstance (); saxparser parser = factory. newsaxparser (); parser. parse (new file ("student. XML "), new myhandler2 () ;}} class myhandler2 extends defaulthandler {private stack <string> stack = new stack <string> (); Private string name; private string gender; private string age; private string pre; @ overridepublic void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {/stack. push (QNAME); Pre = QNAME; For (INT I = 0; I <attributes. getlength (); I ++) {string attrname = attributes. getqname (I); string attrvalue = attributes. getvalue (I); system. out. println (attrname + "=" + attrvalue) ;}@ overridepublic void characters (char [] CH, int start, int length) throws saxexception {// string tag = stack. peek (); string tag = pre; If ("name ". equals (TAG) {name = new string (CH, start, length);} else if ("gender ". equals (TAG) {gender = new string (CH, start, length);} else if ("age ". equals (TAG) {age = new string (CH, start, length) ;}@ overridepublic void endelement (string Uri, string localname, string QNAME) throws saxexception {// stack. pop (); // indicates that the element has been parsed. If ("student" needs to pop up from the stack ". equals (QNAME) {system. out. println ("name:" + name); system. out. println ("Gender:" + gender); system. out. println ("Age:" + age); system. out. println () ;}pre = NULL ;}}

The three methods are described as follows through the image, including tag start, tag intermediate content, tag end, and so on. The "student" tag rules outside are also described.

 

The QNAME parameter indicates the name of each element header in XML. For details, see.

 

Description of the attributes parameter. For details, see.

 

For details about the methods, see JDK. There are also many other methods that can be implemented as needed.

 

 

Appendix:

XML file

<? XML version = "1.0" encoding = "UTF-8"?> <Student Register xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation = "student. XSD "> <student ID =" 1 "> <Name> Zhang San </Name> <gender> male </gender> <age> 20 </age> </student> <student ID = "2"> <Name> Li Si </Name> <gender> female </gender> <age> 19 </age> </student> <student ID =" 3 "> <Name> Wang Wu </Name> <gender> male </gender> <age> 21 </age> </student roster>

 

Output result

Xmlns: xsi = http://www.w3.org/2001/XMLSchema-instancexsi:noNamespaceSchemaLocation = student. XSD student ID = 1 Name: James Gender: male age: 20 student ID = 2 Name: Li Si Gender: Female Age: 19 student ID = 3 name: Wang Wu Gender: male age: 21

 

Note:

The global variable of this row needs to be cleared after this method is completed, because if the global variable is not cleared, it will affect subsequent execution of the program. You can try to skip this line of code.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.