XML parsing-detailed explanation of the code in the SAX parsing process

Source: Internet
Author: User
In the previous article, I thanked the parsing principles and processes. here I used the code to intuitively understand this principle: in the previous article, I thanked the parsing principles and processes. here I used the code to intuitively understand this principle:

Create a Demo1 class:


Import java. io. file; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory;/*** The first SAX program to read xml files * @ author APPle **/public class Demo1 {public static void main (String [] args) throws Exception {// 1. create a SAXParser object // SAXParser parser = SAXParserFactory. newInstance (). newSAXParser (); // 2. call the parse method/*** parameter 1: xml document * parameter 2: The sub-class of DefaultHandler, which defaults to the base class, so it cannot be new. Subclass */parser. parse (new File (". /src/contact. xml "), new MyDefaultHandler (); // use the specified DefaultHandler to parse the content of the specified file into XML. Here, you must customize the MyDefaultHandler class and implement the business logic in it. I don't understand this mode .}}


Next, inherit the DefaultHandler MyDefaultHandler from the custom definition.



Import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler;/*** SAX handler (how to parse the xml document) * @ author APPle **/public class MyDefaultHandler extends DefaultHandler {/*** is called at the beginning of the document */@ Overridepublic void startDocument () throws SAXException {System. out. println ("MyDefaultHandler. startDocument () ");}/*** call * @ param qName when starting the tag: the tag name of the start tag * @ param attributes: the (attribute) contained in the start tag) [list] */@ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {System. out. println ("MyDefaultHandler. startElement () --> "+ qName);}/*** call * @ param qName: Tag name of the end tag */@ Overridepublic void endElement (String uri, string localName, String qName) throws SAXException {System. out. println ("MyDefaultHandler. endElement () --> "+ qName);}/*** call * @ param ch when reading the text: indicates all the text content that has been read. * @ param start: start position of the current text content * @ param length: length of the current text content */@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {// get the current text content String content = new String (ch, start, length); System. out. println ("MyDefaultHandler. characters () --> "+ content);}/*** call */@ Overridepublic void endDocument () throws SAXException {System. out. println ("MyDefaultHandler. endDocument ()");}}

Print the output to learn more about the sax parsing process. The next article provides application cases

The above is a detailed explanation of the code for the SAX parsing process of XML parsing. For more information, see The PHP Chinese website (www.php1.cn )!

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.