XML (3) parsing XML by using Sax and xml by using sax

Source: Internet
Author: User

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.

Related Article

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.