JAVA parses XML using the SAX method, javaxmlsax
JAVA-based XML parsing using the SAX Method
Use the static newInstance () method of SAXParseFactory to obtain the factory of the SAXParserFactory instance
Return the SAXParserFactory instance parser using the newSAXParser () method of the SAXParserFactory instance.
Create a class that inherits DefaultHandle, override the method for business processing, and create the class instance handle
Override the DefaultHandle Class Method
The startElement method is used to traverse the start tag of an xml file;
The endElement method is used to traverse the end tag of an xml file;
The startDocument method is used to identify the start of parsing;
The endDocument method is used to identify the resolution end.
Characters method is used to obtain text
Where: The name of the element retrieved by the qName Parameter
There will also be blank and line breaks.
The following code is directly used: <? Xml version = "1.0" encoding = "UTF-8"?> <Bookstore> <book id = "1"> <name> JAVA Programming ideology </name> <anthor> ***** </anthor> <year> 2000 </year> </book> <book id = "2"> <name> crazy JAVA series </name> <anthor> Li Gang </anthor> <price> 89 </price> </ book> </bookstore>Books. xmlSAX. javapackage pers. zww. xml. handler; import javax. xml. stream. events. startElement; import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; public class SAXParserHandler extends DefaultHandler {int bookIndex = 0;/** Parse XML Element */@ Override public void startElement (String uri, String localName, String qName, Attributes attributes) throws sax1_tio N {super. startElement (uri, localName, qName, attributes); // start to parse the attribute if (qName. equals ("book") {bookIndex ++; System. out. println ("START traversing" + bookIndex + "book"); // you can obtain the attribute name under the specified book element based on the attribute name. // String value = attributes. getValue ("id"); // System. out. println ("book attribute value:" + value); // The attribute name and number of unknown book elements int num = attributes. getLength (); for (int I = 0; I <num; I ++) {System. out. print ("the name of the book element" + (I + 1) + "attribute name :" + Attributes. getQName (I); System. out. println ("& attribute value:" + attributes. getValue (I) ;}} else if (! QName. equals ("book ")&&! QName. equals ("bookstore") {System. out. print ("node name:" + qName);} else {}}@ Override public void characters (char [] ch, int start, int length) throws SAXException {// TODO Auto-generated method stub super. characters (ch, start, length); String val = new String (ch, start, length); if (! Val. trim (). equals ("") {System. out. println ("& node value:" + val) ;}/ ** used to traverse the end tag of an xml file */@ Override public void endElement (String uri, String localName, string qName) throws SAXException {super. endElement (uri, localName, qName); // determines whether a book has been traversed and ended if (qName. equals ("book") {System. out. println ("=============================== ");}} /** indicates the resolution start */@ Override public void startDocument () throws SAXException {// TODO Auto-generated method stub super. startDocument (); // The first line starts System. out. println ("SAX resolution start");}/** indicates the resolution end */@ Override public void endDocument () throws SAXException {// TODO Auto-generated method stub super. endDocument (); // the last row ends System. out. println ("End of SAX Parsing ");}}Comparison between SAXParserHandler. Sort ax and DOM
DOM parsing principle: first load the XML file into the memory and parse it one by one;
Analysis Principle of SAX:You can use the Handler class created by yourself to analyze each node you encounter one by one (node analysis starts from the outermost layer to the internal layer one by one ).
References
MOOC network JessicaJiang teacher video tutorial link: http://www.imooc.com/video/3789