在Java使用Sax解析xml文件中,我们介绍了如何用SAX解析xml文件,接下来我们继续学习如何将一个xml文件的内容结构保存到一个java实例对象中
一、xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="156">
<name>计算机网络</name>
<author>谢希仁</author>
<price>39</price>
<year>2013</year>
</book>
<book id="234">
<name>计算机操作系统</name>
<author>佚名</author>
<price>40</price>
<year>2013</year>
<edition>第四版</edition>
</book>
<book id="367">
<name>计算机组成原理</name>
<price>35</price>
<year>2013</year>
<edition>第三版</edition>
</book>
</bookstore>
二、首先,我们要想写一个与xml中的book节点结构相同的 实体类Book
public class Book {
private String id;
private String name;
private String author;
private String price;
private String year;
private String edition;
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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + ", year=" + year
+ ", edition=" + edition + "]";
}
}
三、编写DefaultHandler类
使用全局变量来实现不同函数之间的对象共享
public class MyHandler extends DefaultHandler {
private String value = null;
private List<Book> bookList = new ArrayList<>();
private Book book = null;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if ("book".equals(qName)) {
book = new Book();
String id = attributes.getValue("id");
book.setId(id);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
value = new String(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
switch (qName) {
case "book":
bookList.add(book);
book = null;
break;
case "name":
book.setName(value);
break;
case "author":
book.setAuthor(value);
case "year":
book.setYear(value);
break;
case "edition":
book.setEdition(<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">value</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">);</span>
break;
case "price":
book.setPrice(value);
break;
}
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
}
四、编写主函数
public class SaxXmlParser {
public static void main(String[] args) {
// 创建SAXParserFactory实例
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
// 创建SAXParser实例
SAXParser parser = spf.newSAXParser();
// 创建DefaultHandler实例
MyHandler handler = new MyHandler();
// 开始解析
parser.parse("book.xml", handler);
for (Book book : handler.getBookList()) {
System.out.println(book);
}
} catch (ParserConfigurationException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Book [id=156, name=计算机网络, author=谢希仁, price=39, year=2013, edition=null]
Book [id=234, name=计算机操作系统, author=佚名, price=40, year=2013, edition=第四版]
Book [id=367, name=计算机组成原理, author=null, price=35, year=2013, edition=第三版]