欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java——使用dom4j和xpath进行解析xml

程序员文章站 2022-05-28 21:02:57
...

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();

2XPath简介

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;
	}
}





相关标签: dom4j xml java