Dom4j-读写xml

1.示例代码

 Document document = DocumentHelper.createDocument();
// 增加命名空间
Namespace sopa12 = Namespace.get("soap12", "http://www.w3.org/2003/05/soap-envelope");

// 添加带命名空间的节点
Element eleSoap12 = document.addElement(new QName("Envelope", sopa12))
		.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
		.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

eleSoap12.addElement(new QName("Body", sopa12)).addElement("GetGeoIP", "http://www.webservicex.net/")
		.addElement("IPAddress").addText("180.102.21.198");

//节点添加xmlns属性
addElement("GetGeoIP", "http://www.webservicex.net/")

// dom转xml string
String requestContent = document.asXML();

//生成的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<soap12:Body>
		<GetGeoIP xmlns="http://www.webservicex.net/">
			<IPAddress>180.102.21.198</IPAddress>
		</GetGeoIP>
	</soap12:Body>
</soap12:Envelope>

2. 解析XML

2.1 逐层解析

//string 转 dom, 字符串转xml
Document document = DocumentHelper.parseText(text);

//读取xml文件
SAXReader reader = new SAXReader();
Document document = reader.read(new File("d:/skills.xml"));
//根节点
Element root = document.getRootElement();
//子节点,elements
Element skill = root.element("skill");
//获取属性
Attribute attr1 = skill.attribute("name");
//节点内容值
node.getTextTrim()

2.2 XPath-推荐

2.2.1 简单不带namespace

    List<Node> list = document.selectNodes("//foo/bar");	//获取多个节点
    Node node = document.selectSingleNode("//foo/bar/author");	//后去单个节点

	第一种形式	/AAA/DDD/BBB: 表示一层一层的,AAA下面 DDD下面的BBB
	第二种形式  //BBB: 表示和这个名称相同,表示只要名称是BBB,都得到
	第三种形式	/*: 所有元素
	第四种形式	BBB[1]: 表示第一个BBB元素
	     BBB[last()]:表示最后一个BBB元素
	第五种形式	//BBB[@id]: 表示只要BBB元素上面有id属性,都得到
	第六种形式	//BBB[@id='b1'] 表示元素名称是BBB,在BBB上面有id属性,并且id的属性值是b1

2.2.2 带命名空间的复杂xml

List<Node> nodeStatus = document.selectNodes("//*[local-name()='status']");
text += "status: " + nodeStatus.get(0).getText() + "
";

2.3 复杂xml解析示例

SAXReader reader = new SAXReader();
Document document = reader.read(new File("C:/Users/z00316474/Desktop/s.xml"));

// 获取status值
Node nodeStatus = document.selectSingleNode("//*[local-name()='status']");
System.out.println("status: " + nodeStatus.getText());

// 获取keyId-第一种方法
Node nodekeyId1 = document.selectSingleNode("//*[local-name()='keyId']");
System.out.println("keyId1: " + nodekeyId1.getText());

// 获取keyId-第二种方法
Node nodekeyId2 = document.selectSingleNode("//*[namespace-uri()='drm:MultiDrmCommon/v1/schemas']");
System.out.println("keyId2: " + nodekeyId2.getText());
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
	<soap:Body>
		<GetKeyAndSignalizationResponse xmlns="drm:KeyAndSignalization/v1/schemas" xmlns:ns2="drm:MultiDrmCommon/v1/schemas">
			<status>OK</status>
			<contentKey>
				<ns2:keyId>acb3a0fa-1484-4870-bc10-0c052957772b</ns2:keyId>
				<ns2:key>aE3kbqlTKRP2HwqlZS+jBA==</ns2:key>
			</contentKey>
			<drmSignalization>
				<ns2:hls>
					<ns2:drmSystemId>adb41c24-2dbf-4a6d-958b-4457c0d27b95</ns2:drmSystemId>
					<ns2:drmName>PRM</ns2:drmName>
					<ns2:keyUri>http://www.nagra.com/key=31&amp;prm=eyJjb250ZW50SWQiOiIzMSIsImtleUlkIjoiYWNiM2EwZmEtMTQ4NC00ODcwLWJjMTAtMGMwNTI5NTc3NzJiIn0</ns2:keyUri>
				</ns2:hls>
			</drmSignalization>
		</GetKeyAndSignalizationResponse>
	</soap:Body>
</soap:Envelope>


4. 官方链接

5. POM

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.0.1</version>
</dependency>

原文地址:https://www.cnblogs.com/Desneo/p/7504931.html