Parse XML and saxxml using SAX
There are three parts:
1,
Package com;
Import java. io. IOException;
Import javax. xml. parsers. ParserConfigurationException;
Import javax. xml. parsers. SAXParser;
Import javax. xml. parsers. SAXParserFactory;
Import org. xml. sax. SAXException;
Import com. ii. SAXParserHandler;
Import com. jj. Book;
Public class SAXTest {
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Obtain an instance of SAXParseFactory
SAXParserFactory factory = SAXParserFactory. newInstance ();
// Get SAXParser through factory
Try {
SAXParser parser = factory. newSAXParser ();
// Create a factory to obtain the SAXParser instance
SAXParserHandler handler = new SAXParserHandler ();
Parser. parse ("books. xml", handler );
System. out. println ("~~~~! Total: "+ handler. getBookList (). size () +" book ");
For (Book book: handler. getBookList ()){
System. out. println (book. getId ());
System. out. println (book. getName ());
System. out. println (book. getAuthor ());
System. out. println (book. getYear ());
System. out. println (book. getPrice ());
System. out. println (book. getLanguage ());
System. out. println ("----------- finish ---------");
}
} Catch (ParserConfigurationException | SAXException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
2,
Package com. ii;
Import java. util. ArrayList;
Import javax. xml. stream. events. StartElement;
Import org. xml. sax. Attributes;
Import org. xml. sax. SAXException;
Import org. xml. sax. helpers. DefaultHandler;
Import com. jj. Book;
Public class SAXParserHandler extends DefaultHandler {
String value = null;
Book = null;
Private ArrayList <Book> bookList = new ArrayList <Book> ();
Public ArrayList <Book> getBookList (){
Return bookList;
}
Int bookIndex = 0;
/*
* Used to traverse the start tag of an XML file
* Used to parse xml elements
**/
@ Override
Public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
// Call the startElement () method of the DefaultHandler class;
Super. startElement (uri, localName, qName, attributes );
// Start parsing the attributes of the book Element
If (qName. equals ("book ")){
// Create a book object
Book = new Book ();
BookIndex ++;
// The attribute name under the book element is known, and the attribute value is obtained based on the attribute name
// String value = attributes. getValue ("id ");
// System. out. println ("the attribute value of book is:" + value );
System. out. println ("================================ start traversing the" + bookIndex + "contents of the book ====== =================================== ");
// If you do not know the name and number of attributes under the book element, how to obtain the attribute name and attribute value
Int num = attributes. getLength ();
For (int I = 0; I <num; I ++ ){
System. out. print ("the name of the book element is + (I + 1) +" + attributes. getQName (I ));
System. out. println ("---- attribute value:" + attributes. getValue (I ));
If (attributes. getQName (I). equals ("id ")){
Book. setId (attributes. getValue (I ));
}
}
}
Else if (! QName. equals ("book ")&&! QName. equals ("bookstore ")){
System. out. print ("node name:" + qName );
}
}
/*
* Used to identify the start of resolution
**/
@ Override
Public void startDocument () throws SAXException {
// TODO Auto-generated method stub
System. out. println ("SAX parsing starts !! ");
Super. startDocument ();
}
/*
* Used to identify the resolution end
**/
@ Override
Public void endDocument () throws SAXException {
// TODO Auto-generated method stub
System. out. println ("The End Of The SAX parsing !! ");
Super. endDocument ();
}
/*
* Used to traverse the end tag of an XML file
**/
@ Override
Public void endElement (String uri, String localName, String qName)
Throws SAXException {
// TODO Auto-generated method stub
Super. endElement (uri, localName, qName );
If (qName. equals ("book ")){
BookList. add (book );
Book = null;
System. out. println ("============================== end the traversal of the" + bookIndex + "book content ======== =================================== ");
}
Else if (qName. equals ("name ")){
Book. setName (value );
}
Else if (qName. equals ("author ")){
Book. setAuthor (value );
}
Else if (qName. equals ("year ")){
Book. setYear (value );
} Else if (qName. equals ("price ")){
Book. setPrice (value );
} Else if (qName. equals ("language ")){
Book. setLanguage (value );
}
}
@ Override
Public void characters (char [] ch, int start, int length)
Throws SAXException {
// TODO Auto-generated method stub
Super. characters (ch, start, length );
Value = new String (ch, start, length );
If (! Value. trim (). equals ("")){
System. out. println ("-- the node value is:" + value );
}
}
}
3,
Package com. jj;
Public class Book {
Private String id;
Private String name;
Private String author;
Private String year;
Private String price;
Private String language;
Public String getId (){
Return id;
}
Public void setId (String id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getAuthor (){
Return author;
}
Public void setAuthor (String author ){
This. author = author;
}
Public String getYear (){
Return year;
}
Public void setYear (String year ){
This. year = year;
}
Public String getPrice (){
Return price;
}
Public void setPrice (String price ){
This. price = price;
}
Public String getLanguage (){
Return language;
}
Public void setLanguage (String language ){
This. language = language;
}
}