jdk工具类____xml解析SAX解析
package com.cnse.test;
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;
}
}
//
package com.cnse.test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* 使用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();
}
}
}
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("src/test.xml");
List<Book> list=saxReader(fis);
for (Book book : list) {
System.out.println(book);
}
}
}
//
package com.cnse.test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* 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(new Date());
} 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;
}
}
//
<?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>
</books>