1 dom4j解析xml文档
DOM4J 是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的 Java 软件都在使用 DOM4J 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 DOM4J。简单易用,采用Java集合框架,并完全支持DOM、SAX和JAXP。
【优点】①大量使用了Java集合类,方便Java开发人员,同时提供一些提高性能的替代方法。
②支持XPath。
③有很好的性能。
【缺点】①大量使用了接口,API较为复杂。
2 所需jar包
下载了dom4j项目之后,解压缩,将其jar包(我的当前版本是dom4j-1.6.1.jar)加入class path下面。(Properties->Java Build Path -> Add External JARs...)。之后就可以使用其提供的API进行编程。(dom4j.1.6.1.jar )。
3 解析的xml文档
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
4 解析xml文档代码
Book.java实体类
package com.wnq.sk.dom4jXML01;
public class Book
{ // 与books.xml中的节点对应
private String category;
private String titleLang;
private String title;
private String author;
private Integer year;
private Double price;
public String getCategory()
{
return category;
}
public void setCategory(String category)
{
this.category = category;
}
public String getTitleLang()
{
return titleLang;
}
public void setTitleLang(String titleLang)
{
this.titleLang = titleLang;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public Integer getYear()
{
return year;
}
public void setYear(Integer year)
{
this.year = year;
}
public Double getPrice()
{
return price;
}
public void setPrice(Double price)
{
this.price = price;
}
public String toString()
{
return "Book [category=" + category + ", titleLang=" + titleLang + ", title=" + title + ", author=" + author
+ ", year=" + year + ", price=" + price + "]";
}
}
解析xml文件类
ReadXMLFile.java
package com.wnq.sk.dom4jXML01;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ReadXMLFile
{
public static void main(String[] args)
throws Exception
{
String fileName = "books.xml";
List<Book> books = ReadXMLFile(fileName);
for (Book b : books)
{
System.out.println(b.toString());
}
}
public static List<Book> ReadXMLFile(String fileName)
throws Exception
{
List<Book> books = new ArrayList<Book>();
Book book = null;
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(fileName));
// 获取根元素
Element rooteElement = document.getRootElement();
List<Element> bookList = rooteElement.elements();
for (int i = 0; i < bookList.size(); i++)
{
book = new Book();
String category = bookList.get(i).attributeValue("category");
book.setCategory(category);
Element titleElement = bookList.get(i).element("title");
String title = titleElement.getText();
String titleLang = titleElement.attributeValue("lang");
book.setTitleLang(titleLang);
book.setTitle(title);
List<Element> authoreElements = bookList.get(i).elements("author");
String author = "";
for (int j = 0; j < authoreElements.size(); j++)
{
author = author + authoreElements.get(j).getText() + "/";
}
book.setAuthor(author);
Element yearElement = bookList.get(i).element("year");
String year = yearElement.getText();
book.setYear(Integer.valueOf(year));
Element priceElement = bookList.get(i).element("price");
String price = priceElement.getText();
book.setPrice(Double.valueOf(price));
books.add(book);
}
return books;
}
}
5 运行结果