books.XML文件:
书籍book.java实体类:
public class Book {
private String id;
private String name;
private String price;
private String author;
private String year;
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = 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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
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;
}
}
SAXPaserHandler继承DefaultHandler类,实现对XML文件的解析:
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.xml.sax.model.Book;
public class SAXPaserHandler extends DefaultHandler {
int bookIndex=0;
String str;
Book book;
ArrayList<Book> bookList = new ArrayList<Book>();
public ArrayList<Book> getBookList() {
return bookList;
}
/*
* 用来解析XML文件元素的
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//调用DefaultHandler类的startElement方法
super.startElement(uri, localName, qName, attributes);
//开始解析book元素的属性
if(qName.equals("book")){
bookIndex++;
book = new Book();
//已知book元素下的属性名称,根据属性名称获取属性值
// System.out.println("book的属性值是:"+attributes.getValue("id"));
//不知book元素下的属性名称,如何获取属性值?下面就是
for(int i=0;i<attributes.getLength();i++){
System.out.println("开始遍历第"+bookIndex+"本书的第"+(i+1)+"个属性名是:"+attributes.getQName(i)+
"--------属性值是:"+attributes.getValue(i));
if(attributes.getQName(i).equals("id"))
book.setId(attributes.getValue(qName));
}
}else if(!qName.equals("book")&&!qName.equals("bookstore")){
System.out.print("节点名是:"+qName+"----");
}
}
/*
* 获取节点值
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
str=new String(ch,start,length);
if(!str.trim().equals(""))
System.out.println("节点值是:"+str);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
//调用DefaultHandler类的endElement方法
super.endElement(uri, localName, qName);
if(qName.equals("book")){
bookList.add(book);
book=null;
System.out.println("================结束遍历第"+bookIndex+"本书内容==============");
}else if(qName.equals("name")){
book.setName(str);
}else if(qName.equals("price")){
book.setPrice(str);
}else if(qName.equals("author")){
book.setAuthor(str);
}else if(qName.equals("year")){
book.setYear(str);
}else if(qName.equals("language")){
book.setLanguage(str);
}
}
/*
* 用来标志解析开始
*/
@Override
public void startDocument() throws SAXException {
System.out.println("SAX解析开始");
super.startDocument();
}
/*
* 用来标志解析结束
*/
@Override
public void endDocument() throws SAXException {
System.out.println("SAX解析结束");
super.endDocument();
}
}
SAXTest.java测试类:
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.xml.sax.handler.SAXPaserHandler;
import com.xml.sax.model.Book;
public class SAXTest {
public static void main(String[] args) {
//获取SAXParserFactory实例
SAXParserFactory spf = SAXParserFactory.newInstance();
//通过spf获取SAXParser的实例
try {
SAXParser paser= spf.newSAXParser();
SAXPaserHandler sp=new SAXPaserHandler();
paser.parse("books.xml", sp);
System.out.println("##########共有"+sp.getBookList().size()+"本书!!!");
//可以遍历输出每本书的属性值
for(Book book:sp.getBookList()){
System.out.println("书的ID是:"+book.getId());
System.out.println("书的书名是:"+book.getName());
System.out.println("书的价格是:"+book.getPrice());
System.out.println("书的作者是:"+book.getAuthor());
System.out.println("书的出版日期是:"+book.getYear());
System.out.println("书的语言是:"+book.getLanguage());
System.out.println("***************本书遍历结束***************");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}