XML (3) parsing XML by using Sax and xml by using sax
Two Resolution Methods:
Dom4j and sax
Analytical thinking:
Book2.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Bookshelves> <book> <title> JAVA </title> <author> XXXXXX </author> <price> 23333 </price> </book> <title> ANDROID </title> <author> XXXXXX </author> <price> 23333 </price> </book> </bookshelf>
Parse a book2.xml file using Sax
Public class SaxDemo {public static void main (String [] args) throws Exception {// get the parser factory SAXParserFactory factory = SAXParserFactory. newInstance (); // obtain the SAXParser parser = factory through the factory. newSAXParser (); // gets the reader XMLReader reader = parser. getXMLReader (); // register the event processor reader. setContentHandler (new MyContentHandler1 (); // parse xmlreader. parse ("book2.xml");} class MyContentHandler1 implements ContentHandler {@ Overridepublic void startDocument () throws SAXException {System. out. println ("document parsing starts") ;}@ Overridepublic void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException {System. out. println ("start tag found... "+ qName) ;}@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {System. out. println (new String (ch, start, length) ;}@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {System. out. println ("end tag found... "+ qName) ;}@ Overridepublic void endDocument () throws SAXException {System. out. println ("end of document Parsing ");} /*************************************** * **************************/@ Overridepublic void setDocumentLocator (Locator locator) {// TODO Auto-generated method stub} @ Overridepublic void startPrefixMapping (String prefix, String uri) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void endPrefixMapping (String prefix) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void ignorableWhitespace (char [] ch, int start, int length) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void processingInstruction (String target, String data) throws SAXException {// TODO Auto-generated method stub} @ Overridepublic void skippedEntity (String name) throws SAXException {// TODO Auto-generated method stub }}
Resolution result:
The result is that the entire xml document is parsed. How can I obtain a TAG body in the document?
Obtain the title of the second book, that is, ANDROID
Public class SaxDemo {public static void main (String [] args) throws Exception {// get the parser factory SAXParserFactory factory = SAXParserFactory. newInstance (); // obtain the SAXParser parser = factory through the factory. newSAXParser (); // gets the reader XMLReader reader = parser. getXMLReader (); // register the event processor reader. setContentHandler (new MyContentHandler2 (); // parse xmlreader. parse ("book2.xml") ;}} class MyContentHandler2 extends DefaultHandler {private String name = null; int count = 0; @ Overridepublic void startElement (String uri, String localName, String qName, attributes attributes) throws SAXException {this. name = qName ;}@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {if ("name ". equals (name) & count ++ = 1) {System. out. println (new String (ch, start, length) ;}@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {name = null ;}}
Result:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.