XML file:
<?xml version= "1.0" encoding= "UTF-8"?><books> <!--root node--><!--The space here is also counted as a node, so books has 5 sub-nodes-- <book id= "Book1" w= "wwwww" ><name><a>aaaaaa</a>bookname1</name><price>10.0 </price></book><book id= "Book2" ><name>bookname2</name><author>bookauthor2 </author></book></books>
Test class:
Package Com.zhang.xml.sax;import Javax.xml.parsers.saxparser;import javax.xml.parsers.saxparserfactory;/* * Sax parsing is a progressive scan of a document while parsing is done while scanning. * Need to create a Org.xml.sax.helpers.DefaultHandler inheriting class */public class Saxparse {public static void main (string[] args) throws Exc eption {SAXParserFactory factory = Saxparserfactory.newinstance (); SAXParser parser = Factory.newsaxparser (); SAXParser handler = new SAXParser ();p arser.parse ("books.xml", Handler); System.out.println (Handler.getbooks ());}}
Subclass of Org.xml.sax.helpers.DefaultHandler
package com.zhang.xml.sax;import java.util.arraylist;import java.util.list;import org.xml.sax.attributes;import org.xml.sax.saxexception;import org.xml.sax.helpers.defaulthandler; import com.zhang.xml.bean.book;public class saxparser extends defaulthandler { private int index = 0;private list<book> books = new Arraylist<book> ();p rivate book book = null;private string value = null;public list<book> getbooks () {return books;} /** * is used to identify the parsing start */@Overridepublic void startdocument () throws saxexception {super.startdocument (); System.out.println ("----------Start parsing document----------");} /** * used to identify the end of parsing */@Overridepublic void enddocument () throws saxexception {super.enddocument (); System.out.println ("----------End document parsing----------");} /** * parsing an XML element * Call this method once each time the start tag of an element is encountered */@Overridepublic void startelement (string uri, String localname, string qname,attributes attributes) throws saxexception { Super.startelement (Uri, localname, qname, attributes), if ("book". Equals (QName)) {index++; SYSTEM.OUT.PRINTLN ("---start parsing" + index + "book label---"); Book = new book (); int size = attributes.getlength (); for (int i=0; i<size; i++) { System.out.print (" + (i + 1) + " attribute name: " + attributes.getqname (i )); System.out.println ("--Property value:" + attributes.getvalue (i)); "id". Equals (Attributes.getqname (i)) {book.setid (Attributes.getvalue (i));}} else {system.out.println ("Encounter" + qName + "start Tag");}} /** * ends a parse XML element * invokes the method once each time the end tag of an element is encountered */@Overridepublic void endelement ( String uri, string localname, string qname) throws saxexception {// todo Auto-generated method stubsuper.endelement (Uri, localname, qname), if ("book". Equals (QName)) {SYSTEM.OUT.PRINTLN ("---end parsing" + index + "book label---") books.add (book);book = null;} else if ("name". Equals (QName)) {book.setname (value);} else if ("Price". Equals (QName)) {book.setprice (value);} else if ("Author". Equals (QName)) {book.setauthor (value);} else {system.out.println ("Encounters" + qName + "end Tag");}} The value of all the elements in the XML document is stored in the/* *ch, and each time the value of an element is encountered, the method is called once */@Overridepublic void characters (char[] Ch, int start, int length) throws saxexception {super.characters (Ch, start, length); value = new string (ch, start, length);if (!value.trim (). Equals ("") ) { //exclude blank position system.out.println ("-->vaLue value: " + value);}}}
Results:
----------Start parsing the document----------Encounter Books start tag---Start parsing 1th book tag---1th property name: Id--> property Value: Book1 2nd property Name: w--> Property value: Wwwww encounters the name start tag encounters a start tag-->value value: Aaaaaa encounters a end tag-->value value: BookName1 encounters price start label-- Value: 10.0---End parsing 1th book label------start parsing 2nd book label---1th property name: Id--> property Value: Book2 encounters the name start tag-- Value: BookName2 encountered author start tag-->value value: BookAuthor2---End parsing 2nd book label---Encounter books end tag----------End document parsing----------[ Book [Id=book1, Name=bookname1, price=10.0, Author=null], book [Id=book2, Name=bookname2, Price=null, author= BOOKAUTHOR2]]
This article is from the "theYllwRvrNo98" blog, make sure to keep this source http://yellowriver.blog.51cto.com/8753974/1673948
Sax parsing of XML documents