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 )!