使用dom4j解析xml文件,并封装为javabean对象

来源:互联网 发布:如何-创建数据搜索引擎 编辑:程序博客网 时间:2024/04/27 23:12

dom4j是一个java的XML api,性能优异、功能强大、易于使用。这里使用dom4j对xml文件进行解析,并完成对文件的封装。

实现对xml文件的解析,主要使用到的是dom4j中的SAXReader类,该类的使用方法如下:

SAXReader 类//构造SAXReaderSAXReader sr = new SAXReader();//加载文件Document doc = sr.read(File file)//Document对象代表XML文件在内存中的印象//取得根元素Element getRootElement()Element.getName()Element.elements() //取得该元素下的所有直接子元素Element.elementText("str") //从一个元素导航到另一个元素,并取出该元素的文本Element.element("str"); //导航到另一个元素Element.attributeValue("str"); //取得该元素对应的属性

该项目中由于使用到dom4j,因此需要将dom4j的jar文件build path到项目中。这里的XML文件使用了w3school中的一个xml例子,这里命名为book.xml,文件内容如下:

<bookstore><book category="COOKING">  <title lang="en">Everyday Italian</title>   <author>Giada De Laurentiis</author>   <year>2005</year>   <price>30.00</price> </book><book category="CHILDREN">  <title lang="en">Harry Potter</title>   <author>J K. Rowling</author>   <year>2005</year>   <price>29.99</price> </book><book category="WEB">  <title lang="en">Learning XML</title>   <author>Erik T. Ray</author>   <year>2003</year>   <price>39.95</price> </book></bookstore>

这里的XML文件并没有使用任何约束文件。但文件中涉及到子元素以及属性,因此需要对属性和子元素进行解析。根据XML文件的内容(将子元素和属性定义为javabean的字段),建立Book类,代码如下;

package cn.myseu.test.xmlparser;public class Book {private String title;private String author;private String year;private String price;private String category;public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}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 String getYear() {return year;}public void setYear(String year) {this.year = year;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}}

上面的代码实现了对Book对象的封装。下面的程序将实现对该XML文件的解析,并将其存放到一个List中,然后打印该List中的所有元素。测试代码如下:

package cn.myseu.test.xmlparser;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 Demo {public static void main(String[] args) throws Exception {SAXReader sr = new SAXReader();Document document = sr.read(new File("src/cn/myseu/test/xmlparser/book.xml"));Element root = document.getRootElement();List<Element> elementList = root.elements();List<Book> bookList = new ArrayList();for (Element e : elementList) {Book book = new Book();book.setTitle(e.elementText("title"));book.setAuthor(e.elementText("author"));book.setYear(e.elementText("year"));book.setPrice(e.elementText("price"));book.setCategory(e.attributeValue("category"));bookList.add(book);}for (Book book : bookList) {System.out.println("title:"+book.getTitle()+"\t category:"+book.getCategory()+"\t author:"+book.getAuthor()+"\t year:"+book.getYear()+"\t price:"+book.getPrice());}}}

如上代码,实现了对XML文件的解析,并完成了对解析元素的封装。然后打印输出。输出结果如下:

title:Everyday Italian category:COOKING author:Giada De Laurentiis year:2005 price:30.00title:Harry Potter category:CHILDREN author:J K. Rowling year:2005 price:29.99title:Learning XML category:WEB author:Erik T. Ray year:2003 price:39.95

至此,整个XML文件的解析工作暂告一段落。这里仅仅完成了对XML文件的解析,因此也只使用到了一个SAXReader类,dom4j还可以实现更多的功能。在以后的博文中会进一步的学习与探讨。

ps:本篇博文仅仅为个人学习的学习笔记博文。

原创粉丝点击