dom4j用于解析xml,可以对xml文件进行操作,dom4j官方地址为dom4j
下面将演示使用dom4j对文件进行增删改查操作
xml原始文件为
<students>
<student id="001">
<name>绘梨衣</name>
<age>20</age>
<gender>女</gender>
</student>
<student id="002">
<name>北原伊织</name>
<age>20</age>
<gender>男</gender>
</student>
</students>
查询操作
代码如下
public void query() throws DocumentException {
//得到解析器
SAXReader reader = new SAXReader();
//指定解析文件
Document document = reader.read(new File("src/student.xml"));
//获取根节点
Element rootElement = document.getRootElement();
//获取所有student元素
List<Element> students = rootElement.elements("student");
for (Element student : students) {
List<Element> list = student.elements();
for (Element element : list) {
System.out.println(element.getText());
}
}
}
这段代码将查询xml文件中的所有student,下面为控制台输出
增加操作
public void add() throws DocumentException, IOException {
//得到解析器
SAXReader reader = new SAXReader();
//指定解析文件
Document document = reader.read(new File("src/student.xml"));
//得到根节点
Element rootElement = document.getRootElement();
//创建一个新的student节点
Element newElement = DocumentHelper.createElement("student");
//为student节点添加属性
newElement.addAttribute("id", "003");
//创建student的子节点
Element name = DocumentHelper.createElement("name");
name.setText("路飞");
Element age = DocumentHelper.createElement("age");
age.setText("18");
Element gender = DocumentHelper.createElement("gender");
gender.setText("男");
//将创建的子节点加入student中
newElement.add(name);
newElement.add(age);
newElement.add(gender);
rootElement.add(newElement);
//将xml进行更新
XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/student.xml"));
xmlWriter.write(document);
xmlWriter.close();
}
这端代码将添加一个新的student,并且student拥有name,age,gender元素,还有一个id属性,运行后的xml文件为
<students>
<student id="001">
<name>绘梨衣</name>
<age>20</age>
<gender>女</gender>
</student>
<student id="002">
<name>北原伊织</name>
<age>20</age>
<gender>男</gender>
</student>
<student id="003">
<name>路飞</name>
<age>18</age>
<gender>男</gender>
</student>
</students>
更新操作
public void update() throws DocumentException, IOException {
//得到解析器
SAXReader reader = new SAXReader();
//指定解析文件
Document document = reader.read(new File("src/student.xml"));
//通过xpath进行查询
Element age = (Element) document.selectSingleNode("//students//student[@id='003']//age");
age.setText("20");
//将xml进行更新
XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/student.xml"));
xmlWriter.write(document);
xmlWriter.close();
}
这段代码使用xpath进行查询(注意:使用xpath需要引入一个jaxen包,链接为jaxen),将student中id为003的age更新为20。更新后xml文件为
<students>
<student id="001">
<name>绘梨衣</name>
<age>20</age>
<gender>女</gender>
</student>
<student id="002">
<name>北原伊织</name>
<age>20</age>
<gender>男</gender>
</student>
<student id="003">
<name>路飞</name>
<age>20</age>
<gender>男</gender>
</student>
</students>
删除操作
public void delete() throws DocumentException, IOException {
//得到解析器
SAXReader reader = new SAXReader();
//指定解析文件
Document document = reader.read(new File("src/student.xml"));
//通过xpath进行查询
Element student = (Element) document.selectSingleNode("//students//student[@id='003']");
//将该student删除
student.getParent().remove(student);
//将xml进行更新
XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/student.xml"));
xmlWriter.write(document);
xmlWriter.close();
}
这段代码使用xpath找到student中id为003的,并将其删除。删除后的结果为
<students>
<student id="001">
<name>绘梨衣</name>
<age>20</age>
<gender>女</gender>
</student>
<student id="002">
<name>北原伊织</name>
<age>20</age>
<gender>男</gender>
</student>
</students>
总结
dom4j使用的并不多,会基本的增删改查即可,遇到不会的操作查询官方文档就行了,再一次的给出dom4j的官方网址dom4j