Example Analysis of SAX parsing process in XML
This article mainly introduces the example analysis of the SAX parsing process in XML, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Create a new 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 parse method / * parameter 1: xml document * parameter 2: subclass of DefaultHandler, default base class, so you can't new. Parses the contents of the specified file to XML using the subclass * / parser.parse (new File (". / src/contact.xml"), new MyDefaultHandler ()); / / using the specified DefaultHandler. Be sure to customize the MyDefaultHandler class here and implement the business logic in it. If you don't understand, die first. Remember this pattern. }}
Then the MyDefaultHandler of custom inheritance DefaultHandler is given.
Import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler / * SAX processor (how to parse xml documents) * @ author APPle * * / public class MyDefaultHandler extends DefaultHandler {/ * call * / @ Override public void startDocument () throws SAXException {System.out.println ("MyDefaultHandler.startDocument ()") when starting a document } / * call * @ param qName when starting the tag: indicates the tag name of the start tag * @ param attributes: indicates the (attribute) [list] * / @ Override public void startElement (String uri, String localName, String qName) contained in the start tag. Attributes attributes) throws SAXException {System.out.println ("MyDefaultHandler.startElement ()-- >" + qName) } / * call * @ param qName when ending the tag: end the tag name of the tag * / @ Override public void endElement (String uri, String localName, String qName) throws SAXException {System.out.println ("MyDefaultHandler.endElement ()->" + qName) } / * call * @ param ch when reading the text content: indicates all the text content currently read * @ param start: indicates the starting position of the current text content * @ param length: indicates the length of the current text content * / @ Override public 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 * / @ Override public void endDocument () throws SAXException {System.out.println ("MyDefaultHandler.endDocument ()");}}
Printout, more intuitive understanding of the sax parsing process.
Thank you for reading this article carefully. I hope the article "sample Analysis of SAX parsing process in XML" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!