1.XML解析技术
(1)dom:Document Object Model,文档对象模型,W3C推荐
(2)sax:Simple API for XML,XML社区的实际标准(有点像OSI和TCP/IP的关系)
2.dom4j简介
简单、灵活的开放源代码的库,是一个非常优秀的Java XML API。dom4j使用接口和抽象基类,因此比jdom更灵活。Hibernate中也采用dom4j。
3.下载和配置dom4j
下载地址:http://sourceforge.net/projects/dom4j/
导入dom4j-1.6.1.jar到项目中(build path)
查阅dom4j的相关API文档
4.DEMO
假设有如下的car.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<carlist>
<car>
<brand produceTime="2010">Audi</brand>
<place>Beijing</place>
<price>30</price>>
</car>
<car>
<brand produceTime="2011">toyota</brand>
<place>Guangzhou</place>>
<price>60</price>
</car>
</carlist>
现在利用dom4j来解析该xml文档
package web.java.xml.dom4j;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 基于dom4j解析XML文件
* @author well
*
*/
public class Demo1 {
public static void main(String[] args) throws DocumentException {
//创建dom4j解析器
SAXReader saxReader = new SAXReader();
//加载需要解析的XML文件
Document doc = saxReader.read(new File("src/web/java/xml/dom4j/car.xml"));
//取得根元素
Element rootElement = doc.getRootElement();
//显示根元素的名称
System.out.println(rootElement.getName());
//取得根元素下的子元素
List<Element> elementList = rootElement.elements();
System.out.println("共有"+elementList.size()+"个子元素");
for (Element e : elementList) {
System.out.println("brand:"+e.elementText("brand"));
System.out.println("place:"+e.elementText("place"));
System.out.println("==============");
}
}
}
在上面的例子中,首先解析XML文件得到根元素,其次通过根元素得到下面的子元素。
(Doucument是一个接口,具体参见dom4j的api文档)
通常调用Document对象下面的getRootElement()方法得到XML文档的根元素
有三种方式获得Document对象:
(1)读取XML文件,获得document对象(解析)
SAXReader saxReader = new SAXReader();
//加载需要解析的XML文件
Document doc = saxReader.read(new File("src/web/java/xml/dom4j/car.xml"));
(2)解析XML形式的文本,得到document对象
String text = "<car>Jeep</car>";
Document document = DocumentHelper.parseText(text);
(3)主动创建document对象(创建)
Document document = DocumentHandler.createDocument();
Element root = document.addElement("car");
5.对XML文件中的指定元素做C.U.D操作
(这里以car.xml为例)
package web.java.xml.dom4j;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
/**
* 使用dom4j操作XML文件的C.U.D(Create\Update\Delete)
*
* @author well
*
*/
public class Demo2 {
/**
* 读取XML文件的公共方法
*
* @throws DocumentException
*/
private Document getDocument() throws DocumentException {
File file = new File("src/web/java/xml/dom4j/car.xml");
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(file);
return document;
}
/**
* 将文件写入XML文件
*
* @param document
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
* @throws IOException
*/
private void write2XML(Document document) throws FileNotFoundException,
UnsupportedEncodingException, IOException {
// 输出格式良好的
OutputFormat oformat = OutputFormat.createPrettyPrint();
OutputStream os = new FileOutputStream("src/web/java/xml/dom4j/car.xml");
XMLWriter xmlWriter = new XMLWriter(os, oformat);
xmlWriter.write(document);
xmlWriter.close();
}
/**
* 在XML文件中新建新的元素(根下面的子元素)
*
* @throws Exception
*/
@Test
public void create() throws Exception {
// 获取Document对象
Document document = getDocument();
Element rootElement = document.getRootElement();
// 获取文件中的第一个元素
Element firstCar = (Element) rootElement.elements().get(0);
firstCar.addElement("price").setText("40w");
write2XML(document);
}
/**
* 更新XML文件中指定元素的值
*
* @throws Exception
*/
@Test
public void update() throws Exception {
Document doc = getDocument();
Element rootElement = doc.getRootElement();
Element firstCar = (Element) rootElement.elements().get(0);
firstCar.element("price").setText("50");
write2XML(doc);
}
/**
* 删除XML文件中的指定元素
* @throws Exception
*/
@Test
public void delete() throws Exception {
Document doc = getDocument();
Element rootElement = doc.getRootElement();
Element firstCar = (Element) rootElement.elements().get(0);
Element firstCarPrice = (Element) firstCar.element("price");
//firstCar.remove(firstCarPrice);
firstCarPrice.getParent().remove(firstCarPrice);
write2XML(doc);
}
}
在上面的例子中,将读取创建document对象 和 写入XML文件 的代码抽取出来,因为在每一个方法中这些代码是重复的。