要使用dom4j必须导入两个jar包:
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
使用dom4j新建一个XML文件:
/**
* 利用dom4j完成新增一个xml文件
* @throws Exception
*/
@Test
public void test_Cread() throws Exception{
// 创建一个空的dom对象
Document dom = DocumentHelper.createDocument();
// 创建一个根标签 persons
Element root = dom.addElement("persons");
// 在root标签中添加一个子标签 person
Element person1 = root.addElement("person");
// 给person添加一个属性 address
person1.addAttribute("address", "上海");
// 添加子标签 name
Element name1 = person1.addElement("name");
name1.setText("张飞");
// 添加子标签 age
Element age1 = person1.addElement("age");
age1.setText("30"); // 创建另外一个person标签
Element person2 = root.addElement("person");
// 给person添加一个属性 address
person2.addAttribute("address", "天津");
// 添加子标签 name
Element name2 = person2.addElement("name");
name2.setText("关羽");
// 添加子标签 age
Element age2 = person2.addElement("age");
age2.setText("25"); // 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close();
}
执行结果:(会在项目的根目录下产生一个:persons.xml文件内容如下)
查询元素:
/**
* 利用dom4j完成查询元素,使用xpath技术查询
* @throws Exception
*/
@Test
public void test_Select() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 获取有 address="天津"属性的person标签
Node node = dom.selectSingleNode("//person[@address='天津']");
// 再获取 当前节点下面的name标签的值
List<Element> nameList = node.selectNodes("./name");
Element name = nameList.get(0);
String nameValue = name.getText();
// 打印的值为关羽
System.out.println(nameValue); }
修改元素的值:
/**
* 利用dom4j完成修改元素的值
* @throws Exception
*/
@Test
public void test_Modify() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 将关羽的年龄改成38
Element age = (Element) dom.selectSingleNode("//person[@address='天津']/age");
age.setText("38"); // 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close();
}
执行结果:
删除节点:
/**
* 利用dom4j完成删除节点
* @throws Exception
*/
@Test
public void test_Delete() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 删除关羽的年龄
// 先获取关羽的节点
Element person = (Element) dom.selectSingleNode("//person[@address='天津']");
// 获取关羽下面的age节点
Node age = person.selectSingleNode("./age");
// 删除节点,用父节点删除子节点
person.remove(age);
// 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close();
}
执行结果(删除了关羽下面的年龄):
CURD的所有代码:
package com.xiaoshitou.test; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.List; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test; /**
* 使用dom4j完成xml文件的CURD
* @author Beck
*
*/
public class TestXML {
/**
* 利用dom4j完成删除节点
* @throws Exception
*/
@Test
public void test_Delete() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 删除关羽的年龄
// 先获取关羽的节点
Element person = (Element) dom.selectSingleNode("//person[@address='天津']");
// 获取关羽下面的age节点
Node age = person.selectSingleNode("./age");
// 删除节点,用父节点删除子节点
person.remove(age); // 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close(); }
/**
* 利用dom4j完成修改元素的值
* @throws Exception
*/
@Test
public void test_Modify() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 将关羽的年龄改成38
Element age = (Element) dom.selectSingleNode("//person[@address='天津']/age");
age.setText("38"); // 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close();
} /**
* 利用dom4j完成查询元素,使用xpath技术查询
* @throws Exception
*/
@Test
public void test_Select() throws Exception{
// 获取dom对象
SAXReader saxReader = new SAXReader();
Document dom = saxReader.read(new FileReader("persons.xml"));
// 获取有 address="天津"属性的person标签
Node node = dom.selectSingleNode("//person[@address='天津']");
// 再获取 当前节点下面的name标签的值
List<Element> nameList = node.selectNodes("./name");
Element name = nameList.get(0);
String nameValue = name.getText();
// 打印的值为关羽
System.out.println(nameValue); } /**
* 利用dom4j完成新增一个xml文件
* @throws Exception
*/
@Test
public void test_Cread() throws Exception{
// 创建一个空的dom对象
Document dom = DocumentHelper.createDocument();
// 创建一个根标签 persons
Element root = dom.addElement("persons");
// 在root标签中添加一个子标签 person
Element person1 = root.addElement("person");
// 给person添加一个属性 address
person1.addAttribute("address", "上海");
// 添加子标签 name
Element name1 = person1.addElement("name");
name1.setText("张飞");
// 添加子标签 age
Element age1 = person1.addElement("age");
age1.setText("30"); // 创建另外一个person标签
Element person2 = root.addElement("person");
// 给person添加一个属性 address
person2.addAttribute("address", "天津");
// 添加子标签 name
Element name2 = person2.addElement("name");
name2.setText("关羽");
// 添加子标签 age
Element age2 = person2.addElement("age");
age2.setText("25"); // 保存xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileWriter("persons.xml"), format);
writer.write(dom);
writer.close();
}
}