Move from the URL below. http://hck.iteye.com/blog/1175762
However, the procedure has been slightly altered.
About sax (moving from Wikipedia)
SAX, the universal simple API for XML, refers to both an interface and a software package.
Sax is an event-driven A standard interface for XML parsing does not change the way sax works simply by sequentially scanning the document, notifying the event handler when the document begins and ends, the element starts and ends, the document ends, and so on. The event handler acts accordingly, and then continues the same scan until the end of the document. Most sax produces the following types of events: 1. Triggers a document processing event at the beginning and end of a document. 2. An element event is triggered before and after each XML element in the document accepts resolution. 3. Any metadata is usually handled by a separate event 4. A DTD or schema event is generated when processing a document's DTD or schema. 5. Error events are used to notify the host of application parsing errors.
common interfaces for sax
ContentHandler interface
The
ContentHandler is a special sax interface in the Java class package, located in the Org.xml.sax package. The interface encapsulates a number of methods for event handling, and when the XML parser begins parsing the XML input document, it encounters special events such as the beginning and end of the document, the beginning and end of the element, and the character data in the element. When these events are encountered, the XML parser invokes the corresponding method in the ContentHandler interface to respond to the event. The ContentHandler interface has the following methods: void Startdocument () void enddocument () void startelement (String uri, string LocalName, String QName, Attributes atts) void EndElement (String uri, String localname, string qName) void characters (char& #91; ; amp; #93; CH, int start, int length) Dtdhandler interface, Entityresolver interface, ErrorHandler interface, these three interfaces are not used. Sax Parsing Document Process example for an XML document for example <doc> < Para>hello,world!</para> </doc> Its parsing process is: 1.start document 2.start Element:doc ... 3.start Element:para ... 4.characters:hello,world! 5.end Element:para ... 6.end Element;doc ... 7.end document an event occurs for each step in the parsing process, triggering an event handler in the corresponding interface. said almost, look at a program.
<?xml version= "1.0" encoding= "GB2312"?> <books count= "3" xmlns= "Http://test.org/books" >
<!--Books ' s comment-->
<book id= "1" >
<name>thinking in java</name>
<price >11</price>
</book>
<book id= "2" >
<name>core java2</name>
< price>22</price>
</book>
<book id= "3" >
<name>c++ primer</name>
<price>33</price>
</book>
</books>
Book.java public
class book {
private int id;
private String name;
private double price;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public double GetPrice () {return price
;
}
public void Setprice (double price) {
This.price = Price;
}
Public String toString () {return
' id = ' +id + ', name = ' + name + ', Price = ' +price
}
} '
Booksdefaulthandler.java import java.util.ArrayList;
Import java.util.List;
Import org.xml.sax.Attributes;
Import org.xml.sax.SAXException;
Import Org.xml.sax.helpers.DefaultHandler;
public class Booksdefaulthandler extends defaulthandler{private list<book> Booklist;
Private book Currentbook;
Private String tag;
@Override public void Startdocument () throws saxexception {Booklist = new arraylist<book> (); @Override public void Startelement (string uri, String localname, String qName, Attributes attribute s) throws Saxexception {if ("book". Equals (LocalName)) {Integer id = new Integer (attributes.getvalue (0)
);
Currentbook = new book ();
Currentbook.setid (ID);
tag = LocalName; @Override public void characters (char[] ch, int start, int length) throws Saxexception {if (Tag!= null) {String data = new String (ch, start,length);
if ("Name". Equals (Tag)) {currentbook.setname (data);
}else if ("Price". Equals (Tag)) {Double Price = new Double (data);
Currentbook.setprice (price); @Override public void EndElement (string uri, String localname, String qName) throws
saxexception {if ("book". Equals (LocalName)) {Booklist.add (Currentbook);
Currentbook = null;
tag = null;
Public list<book> getbooklist () {return booklist; }
}
Saxbook.java
import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import java.util.List;
Import Org.xml.sax.ContentHandler;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;
Import Org.xml.sax.XMLReader;
Import org.xml.sax.helpers.XMLReaderFactory;
public class Saxbook {public
static void Main (string[] args) throws FileNotFoundException, IOException, Saxexceptio n{
Booksdefaulthandler ContentHandler = new Booksdefaulthandler ();
XMLReader reader = Xmlreaderfactory.createxmlreader ();
Reader.setcontenthandler (ContentHandler);
Reader.parse (new InputSource) (New FileReader ("D:/90.temp/books.xml"));
List Booklist = Contenthandler.getbooklist ();
for (Object book:booklist) {
System.out.println (book) book);}
}
Output:
id = 1,name = thinking in Java,price = 11.0
id = 2,name = Core Java2,price = 22.0
id = 3,name = c + + Primer , Price = 33.0