Dom4j是一种用于解析、写入XML文件的Java API,同时由于是开源的,所以可以在GitHub上找到源码。
对于XML的解析,Dom4j很强大也很好用,并且他的性能、功能和易用性都是非常的好,所以你可以尝试一下使用它。
使用Dom4j开发,需要使用到jar包,在下面的地址可以下载:
1、http://www.dom4j.org/dom4j-1.6.1/
2、也可以在我的csdn下载栏中找到资源。
代码如下:
package com.vgbh;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jXMLDemo {
public static void main(String[] args) {
Dom4jXMLDemo dxd = new Dom4jXMLDemo();
String uri = "bookstore.xml";
String uri1 = "F:/WorkSpace/dom4jDemoC.xml";
dxd.queryDom4jXMLDemo(uri);
//dxd.insertDom4jXMLDemo(uri1);
}
//读取XML文件
public void queryDom4jXMLDemo(String uri) {
try {
//构建SAXReader对象,并读取XML文件并转换为doc
SAXReader sReader = new SAXReader();
Document doc = sReader.read(uri);
//获取根节点
Element root = doc.getRootElement();
//获取元素
List<Element> books = root.elements();
for (int i = 0; i < books.size(); i++) {
//创建一个book对象保存数据
Book book = new Book();
//获取节点的属性值赋给book
Element element = books.get(i);
book.setId(element.attributeValue("id"));
book.setName(element.elementText("name"));
book.setAuthor(element.elementText("author"));
book.setPrice(element.elementText("price"));
book.setLanguage(element.elementText("language"));
book.setCountry(element.elementText("country"));
System.out.println("id:" + book.getId() + " name:" + book.getName() + " author:" + book.getAuthor() + " price:" + book.getPrice() +
" language:" + book.getLanguage() + " country:" + book.getCountry());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
//写入XML文件
public void insertDom4jXMLDemo (String uri) {
Document doc = DocumentHelper.createDocument();
//增加根节点
Element bookstore = doc.addElement("bookstore");
//增加子元素
Element book1 = bookstore.addElement("book");
//添加子元素的数据名称
Element name1 = book1.addElement("name");
Element author1 = book1.addElement("author");
Element price1 = book1.addElement("price");
Element language1 = book1.addElement("language");
Element country1 = book1.addElement("country");
book1.addAttribute("id", "1");
name1.setText("一生有你");
author1.setText("午歌");
price1.setText("25");
language1.setText("chinese");
country1.setText("china");
//将Doc转换为XML文件
OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndent(" ");//设置缩进格式
format.setEncoding("UTF-8");//设置XML文件的编码格式
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileOutputStream(uri),format);
writer.write(doc);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Book {
private String id ;
private String name ;
private String author ;
private String price ;
private String language ;
private String country ;
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 getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
1、queryDom4jXMLDemo的结果:
id:1 name:changedName author:college-English price:55 language:english country:2
id:2 name:cet6 author:me price:33 language:lalalal country:chinese
从结果中可以看出,在解析XML文件时,他是将元素返回为对象,再通过获取对象的值从而获得数据。
我在代码中使用了内部类的方式接受传回来的数据,这样保存数据会方便很多,不过需要即用即销。
2、insertDom4jXMLDemo的结果:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>一生有你</name>
<author>午歌</author>
<price>25</price>
<language>chinese</language>
<country>china</country>
</book>
</bookstore>
结果显示的很完美,doc树正常显示,编码格式也没有问题。
有问题的可以联系我的邮箱。