看了这么多的博客,对这个DOM4J还是不太清楚,于是不得不动手记一下笔记,如果哪里有错的还请提出来,共同进步。
如果使用DOM4J应该先导入必要的包,下载地址:
http://download.youkuaiyun.com/detail/chaogu94/9617730
最近分不太够了,如果从这篇博客学到东西的朋友权当打赏一下了,要是没有分数的兄弟需要的话也可以留言联系我,看到后肯定也会发你一份。
Demo1:
xml文件:users.xml
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="1">
<username>张三</username>
<password>123</password>
</user>
<user id="2">
<username>李四</username>
<password>321</password>
</user>
</users>
java代码:
public class Dom4JTest2 {
public static void main(String[] args) throws DocumentException {
// 创建SAXReader对象为了创建document对象
SAXReader reader = new SAXReader();
// 创建document对象
Document document = reader.read("users.xml");
// 创建根节点
Element root = document.getRootElement();
// System.out.println(root.getName());//得到当前节点的节点名
// 获取根节点下所有名为user的节点
List<Element> elements = root.elements("user");
// 遍历出所有子节点
for (Element element : elements) {
// System.err.println(element.getName());
// 得到当前节点下的属性
System.out.println(element.attributeValue("id"));
// 获得当前节点下的子节点的元素的文本值
System.out.println("username:" + element.elementText("username")
+ " password:" + element.elementText("password"));
}
}
}
Demo2:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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.XMLWriter;
public class Dom4JTest2 {
public static void main(String[] args) {
XMLWriter writer = null;
// 首先使用DocumentHelper创建Document对象
Document document = DocumentHelper.createDocument();
// 创建根节点
Element root = document.addElement("users");
// 为根节点添加子元素
Element user1Element = root.addElement("user");
Element user2Element = root.addElement("user");
// 为子元素添加属性
user1Element.addAttribute("id", "1");
user2Element.addAttribute("id", "2");
// 为子元素添加子节点及文本
user1Element.addElement("username").addText("zhangsan");
user1Element.addElement("password").addText("123");
user2Element.addElement("username").addText("lisi");
user2Element.addElement("password").addText("321");
// 文件保存路径
String path = "xml/user.xml";
try {
// 创建XMLWriter来写数据
writer = new XMLWriter(new FileWriter(path),
OutputFormat.createPrettyPrint());
// 将节点写到xml文档中
writer.write(document);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
XPath的教程相应的API中有详细教程,我提供的工具里有,在此就不在重复介绍
关于使用XPath的Demo
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<title>图书A</title>
<author>作者A</author>
<price>89.00</price>
</book>
<book id="2">
<title>图书B</title>
<author>作者B</author>
<price>69.00</price>
</book>
<book id="3">
<title>图书C</title>
<author>作者C</author>
<price>29</price>
</book>
</books>
Java文件
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4JTest2 {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read("users.xml");
Element root = document.getRootElement();
// 使用xpath来查找xml里的节点
// 相对路径查找,从当前节点查找子节点book
List<Element> elements = root.selectNodes("book");
// System.out.println(elements.size());// 结果为3
// 使用绝对路径来查找,从根books节点查找book节点(此时不会去查找book下的子节点)
elements = root.selectNodes("/books/book");
// System.out.println(elements.size());// 结果为3
// 根节点不是book,所以无法查找
elements = root.selectNodes("/book");
// System.out.println(elements.size() + ":" + elements);
// 表示从文档中遍历所有满足要求的节点(从根节点开始)不太建议使用,因为效率不高
elements = root.selectNodes("//title");
// System.out.println(elements.size() + ":" + elements);
// 查找了所有作者为作者B的书
elements = root.selectNodes("book[author='作者B']");
for (Element element : elements) {
//System.out.println("title:"+element.elementText("title"));
}
//查找了属性中id大于等于2的书
elements = root.selectNodes("/books/book[@id>2]");
for (Element element : elements) {
//System.out.println("title:"+element.elementText("title"));
}
//查找名称中包含有 图书 的price节点
elements = root.selectNodes("/books/book[contains(title,'图书')]/price");
for (Element element : elements) {
//System.out.println(element.getTextTrim());
}
//查找名称中包含有图书并且价格小于50的书
elements = root.selectNodes("/books/book[contains(title,'图书') and price>50]");
for (Element element : elements) {
System.out.println(element.elementText("title")+":"+element.elementText("price"));
}
}
}