Write a sax parsing XML document that belongs to you

Source: Internet
Author: User

First you need an XML document
<?xml version= "1.0" encoding= "UTF-8"?>< Bookshelf >  < book >    < title >hack36 meter </title >    < Price >78</Price >    < author > laosi </author >  </book > <  book > <    title >android Advanced Programming < /title >    < price >89</Price >    < author > Leave your windbreaker </author >  </book >  < book >    < title >c++ Advanced Architecture </title >    < price >56</Price >    < author > depressed lifelong </author >  </book ></bookshelf >

Then use the program to parse it, the main process of parsing is to obtain the title of the third book


Describe the parsing process of sax in detail

Sax parsing is the parsing of tag names, using the name of the tag to parse

The process of parsing is to have a reader to read the XML document,

Then there is a content trigger to process the resulting content,

So the process of Sax parsing is as follows

Package Com.jack.xml;import Java.io.ioexception;import Javax.xml.parsers.parserconfigurationexception;import Javax.xml.parsers.saxparser;import Javax.xml.parsers.saxparserfactory;import Org.xml.sax.Attributes;import Org.xml.sax.contenthandler;import Org.xml.sax.locator;import Org.xml.sax.saxexception;import Org.xml.sax.xmlreader;public class Xmlparsetest {//Create Content Resolution object ContentHandler handler = new ContentHandler () {String name = n Ull;int count = 0; @Overridepublic void Startdocument () throws Saxexception {//TODO auto-generated method STUBSYSTEM.OUT.P Rintln ("Start parsing to document");} @Overridepublic void Enddocument () throws Saxexception {//TODO auto-generated method StubSystem.out.println ("End of document parsing");}  @Overridepublic void Startelement (String uri, String localname, String Qname,attributes atts) throws Saxexception {//TODO Auto-generated method stubthis.name = QName;}  @Overridepublic void characters (char[] ch, int start, int length) throws Saxexception {//TODO auto-generated method stubif ("title". Equals (Name) {count++;//system.out.println ("count=====>" +count), if (count = = 3) {System.out.println (New String (CH, start, length));}}} @Overridepublic void EndElement (String uri, String localname, String qName) throws Saxexception {//TODO auto-generated met Hod stubthis.name = "";} @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 Ignorab  Lewhitespace (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}};p ublic static Voi D main (string[] args) {  Xmlparsetest test = new Xmlparsetest (); Test.saxxmlparse ();}  public void Saxxmlparse () {//Create parse factory saxparserfactory factory = Saxparserfactory.newinstance ();//Get parser saxparser parser = null;try {parser = Factory.newsaxparser ()} catch (Parserconfigurationexception e) {//TODO auto-generated catch BLOCKE.P Rintstacktrace ();} catch (Saxexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}  Get reader XmlReader reader = null;try {reader = Parser.getxmlreader ();} catch (Saxexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Set content trigger Reader.setcontenthandler (handler);//Parse document try {reader.parse ("books.xml");} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} catch (Saxexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Write a sax parsing XML document that belongs to you

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.