Java——使用dom4j和xpath进行解析xml
1. Dom4j常用的用法
-解析XML文件,得到xml文件的document对象:
//注意:路径src前面没有使用/符号
String path = "src/main/resources/students.xml";
SAXReader reader = new SAXReader();
Document document = reader.read(path);
-获取根节点:
Element root = document.getRootElement();
-获取某节点的某个子节点(如果存在多个,只返回第一个):
Element childNode = parentNode.element("childNodeName");
-获取某节点的所有子节点:
List<Element> elemList = parentNode.elements("childNodeName");
-获取某节点的对应元素节点,及获取/修改元素节点的属性值:
Attribute attribute = elem.Attribute("attributeName");
String attributeValue = attribute.getText();
attribute.setText("newValue");
-获取/修改某节点的属性值:
String nodeValue = element.getText();
element.setText(newValue);
-添加元素节点:
parent.addAttribute("attributeName", attributeValue);
-添加节点:
Element childNode = parent.addElement("nodeName");
-移除节点:
Element parentNode = elem.getParent();
parentNode.remove(elem);
-将document对象回写到xml文件中:
// 带缩进的风格
OutputFormat format = OutputFormat.createPrettyPrint();
// 压缩风格OutputFormat.createCompactFormat()
format.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format);
xmlWriter.write(document);
xmlWriter.close();
2. XPath简介
XPath使用路径表达式来选取xml文档中的节点(Node)或节点集。
因此,可以通过这种方式使用路径表达式一次性定位到节点,在查找某节点时使用XPath会更加方便。
3. XPath路径表达式语法
-如果路径以斜线/开头,那么该路径表示节点的绝对路径:
/root/A/B
-如果路径以双斜线//开头,则表示任意层次关系的节点:
//B/C (/root/A/B/C、/root/B/C都会被选择)
-星号*表示路径下的所有节点:
/root/A/*
-通过中括号[]指定节点集中的指定位置的节点:(1表示第一个,last()表示最后一个)
/root/A/B[1]
/root/A/B[last()]
-通过中括号[]指定带元素属性的节点:
/root/A/B[@id]
/root/A/B[@id=”attributeValue”]
4. Dom4j结合XPath使用的常用方法
-通过Document的实例对象获取节点或节点集合
-获取文档中的节点集合:
List<Node> nodeList = document.selectNodes(xpath);
-获取节点:
Node node = document.selectSingleNode(xpath);
-通过某节点获取子节点或子节点的集合:(如果xpath不以斜杠/开头,则表示相对于该节点的相对路径):
//node.selectNodes(xpath)
//node.selectSingleNode(xpath)
-获取节点的值:
String nodeValue = node.getText();
-获取节点的元素节点的属性值:
String attributeValue = node.valuesOf("@attributeName");
5. 简单的案例
5.1 引入相关依赖
引入jar包:
dom4j-#.jar(dom4j),jaxen-#.jar(xpath)
或者,pom文件引入依赖:<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
5.2 xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="student001">
<name>zhangsan</name>
<age>18</age>
</student>
</students>
5.3 使用Dom4jUtils类封装了两个static方法:
Document getDocumentFromPath(String path):通过xml文件路径获取document对象
void xmlWrite(String path, Document document):将document对象回写到指定路径的xml文件中
package com.lin.utils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jUtils {
public static Document getDocumentFromPath(String path) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(path);
return document;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
public static void xmlWrite(String path, Document document) {
try {
OutputFormat format = OutputFormat.createPrettyPrint();
//OutputFormat.createCompactFormat()
format.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path), format);
xmlWriter.write(document);
xmlWriter.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.4 对xml文件进行解析,增加删除遍历操作(不使用XPath)
package com.lin.service;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import com.lin.pojo.Student;
import com.lin.utils.Dom4jUtils;
public class StuService {
//注意:路径src前面没有使用/符号
public static final String PATH = "src/main/resources/students.xml";
public static List<Student> getAllStudent(){
List<Student> list = new ArrayList<Student>();
Document document = Dom4jUtils.getDocumentFromPath(PATH);
Element root = document.getRootElement();
List<Element> elemList = root.elements("student");
for (Element elem : elemList) {
Attribute id = elem.attribute("id");
String idv = id.getText();
Element name = elem.element("name");
String namev = name.getText();
Element age = elem.element("age");
String agev = age.getText();
//System.out.println(idv+"-"+namev+"-"+agev);
Student stu = new Student();
stu.setId(idv);
stu.setName(namev);
stu.setAge(Integer.parseInt(agev));
list.add(stu);
}
return list;
}
public static void addStudent(Student stu) {
Document document = Dom4jUtils.getDocumentFromPath(PATH);
Element root = document.getRootElement();
Element student = root.addElement("student");
student.addAttribute("id", stu.getId());
Element name = student.addElement("name");
name.setText(stu.getName());
Element age = student.addElement("age");
age.setText(String.valueOf(stu.getAge()));
//回写xml
Dom4jUtils.xmlWrite(PATH, document);
}
public static Student deleteStudent(String idv) {
Student stu = null;
Document document = Dom4jUtils.getDocumentFromPath(PATH);
Element root = document.getRootElement();
List<Element> elemList= root.elements("student");
for (Element elem : elemList) {
Attribute idAttr = elem.attribute("id");
String id = idAttr.getText();
if(id.equals(idv)) {
Element name = elem.element("name");
String namev = name.getText();
Element age = elem.element("age");
int agev = Integer.parseInt(age.getText());
stu = new Student();
stu.setId(idv);
stu.setName(namev);
stu.setAge(agev);
elem.getParent().remove(elem);
}
}
Dom4jUtils.xmlWrite(PATH, document);
return stu;
}
}
5.5 对StuService中的查询/删除方法进行修改
(需要查找某节点的方法都可以进行xpath方式的修改)
package com.lin.service;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import com.lin.pojo.Student;
import com.lin.utils.Dom4jUtils;
public class StudentService {
public static final String PATH = "src/main/resources/students.xml";
public static List<Student> getAllStudent(){
List<Student> list = new ArrayList<Student>();
Document document = Dom4jUtils.getDocumentFromPath(PATH);
List<Node> nodeList = document.selectNodes("//student");
for (Node node : nodeList) {
String idv = node.valueOf("@id");
Node name = node.selectSingleNode("name");
String namev = name.getText();
Node age = node.selectSingleNode("age");
int agev = Integer.valueOf(age.getText());
System.out.println(idv+"-"+namev+"-"+agev);
Student stu = new Student();
stu.setId(idv);
stu.setName(namev);
stu.setAge(agev);
list.add(stu);
}
return list;
}
//add方法没有使用Xpath的方式
public static void addStudent(Student stu) {
Document document = Dom4jUtils.getDocumentFromPath(PATH);
Element root = document.getRootElement();
Element student = root.addElement("student");
student.addAttribute("id", stu.getId());
Element name = student.addElement("name");
name.setText(stu.getName());
Element age = student.addElement("age");
age.setText(String.valueOf(stu.getAge()));
//回写xml
Dom4jUtils.xmlWrite(PATH, document);
}
public static Student deleteStudent(String idv) {
Student stu = new Student();
Document document = Dom4jUtils.getDocumentFromPath(PATH);
Element root = document.getRootElement();
String xpath = "//student[@id=\""+idv+"\"]";
Node stuNode = document.selectSingleNode(xpath);
String namev = stuNode.selectSingleNode("name").getText();
int agev = Integer.valueOf(stuNode.selectSingleNode("age").getText());
stu.setId(idv);
stu.setName(namev);
stu.setAge(agev);
root.remove(stuNode);
//回写xml
Dom4jUtils.xmlWrite(PATH, document);
return stu;
}
}
上一篇: 人参对中医的作用是什么,人参如何中医养生
下一篇: 最好吃的素菜做法有哪些
推荐阅读
-
java使用dom4j生成与解析xml文档的方法示例
-
java基础 xml 使用dom4j解析 xml文件 servlet根据pattern 找到class
-
Dom4J解析使用xpath java.lang.NoClassDefFoundError: org/jaxen/JaxenException异常
-
Dom4J解析使用xpath java.lang.NoClassDefFoundError: org/jaxen/JaxenException异常
-
Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
-
Java基础之XML介绍与SAX解析、DOM解析XML、JDOM解析、DOM4J解析、XMLEncoder与XMLDecoder的使用以及xstream工具的使用 189~195
-
【XML解析】使用Dom4j对XML进行SAX解析
-
java XML 通过BeanUtils的population为对象赋值 根据用户选择进行dom4j解析
-
Java使用dom方式解析和操作xml,递归解析xml以yml格式输出
-
使用 XPATH 和 HTML Cleaner 解析 HTML/XML(Using XPATH and HTML Cleaner to parse HTML / XML)_html/css_WEB-ITnose