package xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import model.Book;
/**
* 使用SAX解析XML文件
*/
public class SaxHelper {
private static SAXParserFactory spf;
/**
* 使用SAX方式解析XML文件
* @param is XML文件输入流
* @return
*/
public static List<Book> saxReader(InputStream is) {
try {
if (spf == null)
spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
XmlContentHandler xmlContentHandler = new XmlContentHandler();
saxParser.parse(is, xmlContentHandler);
return xmlContentHandler.getBooks();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package xml;
import java.util.ArrayList;
import java.util.List;
import model.Book;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import util.DateUtils;
/**
* SAX解析XML文件的事件处理器
*/
public class XmlContentHandler extends DefaultHandler {
private Book book; //保存每一个节点的对象
private List<Book> books; //保存已解析好的节点对象
private String preTag; //保存当前正在解析的节点标签名
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析文档...");
books = new ArrayList<Book>();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (name.equals("book") && book == null) {
book = new Book();
book.setIsbn(attributes.getValue("", "isbn"));
}
preTag = name; //记录当前开始节点名
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (book != null && preTag != null && !"book".equals(preTag) && !"books".equals(preTag)) {
String data = new String(ch,start,length);
if (!"".equals(data.trim())) {
if ("name".equals(preTag)) {
book.setName(data);
} else if ("author".equals(preTag)) {
book.setAuthor(data);
} else if ("publishing".equals(preTag)) {
book.setPublishing(data);
} else if ("pubdate".equals(preTag)) {
book.setPubdate(DateUtils.string2Date("yyyy-mm-dd", data));
} else if ("price".equals(preTag)) {
book.setPrice(Double.parseDouble(data));
}
}
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if ("book".equals(name) && book != null) {
books.add(book);
book = null; //一个节点的结束,代表一本书的信息解析完成,所以要将book设为null
}
preTag = null; //同时将当前书的节点设为null
}
@Override
public void endDocument() throws SAXException {
System.out.println("XML文件解析结束...");
System.out.println("结果:" + books);
}
/**
* 获得解析后的数据
* @return
*/
public List<Book> getBooks() {
return books;
}
}
package model;
import java.util.Date;
/**
* 图书
*/
public class Book {
/**
* 图书编号
*/
private String isbn;
/**
* 书名
*/
private String name;
/**
* 作者
*/
private String author;
/**
* 出版社
*/
private String publishing;
/**
* 发布时间
*/
private Date pubdate;
/**
* 价格
*/
private double price;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
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 getPublishing() {
return publishing;
}
public void setPublishing(String publishing) {
this.publishing = publishing;
}
public Date getPubdate() {
return pubdate;
}
public void setPubdate(Date pubdate) {
this.pubdate = pubdate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "name: " + name + " isbn: " + isbn + " author: " + author
+ " publishing: " + publishing + " pubdate: " + pubdate
+ " price: " + price;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!-- ISBN:国际标准书号由13位数字组成。前三位数字代表图书,中间的9个数字分为三组,分别表示组号、出版社号和书序号,最后一个数字是校验码。 -->
<book isbn="9787121025389">
<name>JAVA面向对象编程</name>
<author>孙卫琴</author>
<publishing>电子工业出版社</publishing>
<pubdate>2006-07-01</pubdate>
<price>50.60</price>
</book>
<book isbn="9787111349662">
<name>深入理解Java虚拟机:JVM高级特性与最佳实践</name>
<author>周志明</author>
<publishing>机械工业出版社</publishing>
<pubdate>2011-07-01</pubdate>
<price>46.20</price>
</book>
<book isbn="9787508338408">
<name>Java网络编程(第3版)</name>
<author>(英)哈诺德 著 朱涛江,林剑 译</author>
<publishing>中国电力出版社</publishing>
<pubdate>2005-11-01</pubdate>
<price>63.80</price>
</book>
<book isbn="9787508379661">
<name>Java经典实例(第2版)</name>
<author>(美)达尔文(Darwin,F.I) 著 关丽荣,张晓坤 译</author>
<publishing>中国电力出版社</publishing>
<pubdate>2009-02-01</pubdate>
<price>72.10</price>
</book>
<book isbn="9787111213826">
<name>Java编程思想(第4版)</name>
<author>(美)埃克尔 著 陈昊鹏 译</author>
<publishing>机械工业出版社</publishing>
<pubdate>2007-06-01</pubdate>
<price>74.50</price>
</book>
<book isbn="9787508344980">
<name>Head First Java(中文版)(第2版)</name>
<author>(美)塞若(Sierra,K.),(美)贝茨(Bates,B.) 著 O’Reilly Taiwan公司 译 张然,等 编</author>
<publishing>中国电力出版社</publishing>
<pubdate>2007-02-01</pubdate>
<price>55.20</price>
</book>
<book isbn="9787111231837">
<name>数据结构与算法分析:Java语言描述(第2版)</name>
<author>(美)韦斯(Weiss,M.A.) 著 冯舜玺 译</author>
<publishing>机械工业出版社</publishing>
<pubdate>2009-01-01</pubdate>
<price>41.20</price>
</book>
</books>