java解析xml
xml是java工程里常用的配置文件格式,具有很强的表达力。dom4j是经典的xml解析器,本文以此为例,记录java解析xml的过程。
一、创建
$ mvn archetype:generate
# mu模板就用最常用的maven-archetype-quickstart
Confirm properties configuration:
groupId: cn.edu.zju
artifactId: dom4jdemo
version: 1.0-SNAPSHOT
package: cn.edu.zju
Y: : y
二、pom依赖项
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- 在xPath解析式要依赖这个包 -->
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
三、工程结构
➜ dom4jdemo tree
.
├── pom.xml
├── resources
│ └── test.xml # 待解析的xml文件
└── src
├── main
│ └── java
│ └── cn
│ └── edu
│ └── zju
│ └── App.java
└── test
└── java
└── cn
└── edu
└── zju
└── AppTest.java
四、待解析的xml文件
test.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<product name="QQ">
<account id="123456789">
<nickname>qq1</nickname>
<password>123</password>
<level>56</level>
</account>
<account id="987654321">
<nickname>qq2</nickname>
<password>456</password>
<level>12</level>
</account>
</product>
<product name="Netease">
<account id="Tom">
<password>pwdOfTom</password>
<capacity>1024</capacity>
</account>
<account id="Jim">
<password>pwdOfJim</password>
<capacity>2560</capacity>
</account>
</product>
</resources>
五、代码
App.java
package cn.edu.zju;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.xpath.DefaultXPath;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* Hello world!
*/
public class App {
private Document document;
App() {
SAXReader reader = new SAXReader();
try {
document = reader.read("resources/test.xml");
} catch (DocumentException e) {
e.printStackTrace();
}
}
void xPathParse() {
// 用xpath解析
XPath xPath = new DefaultXPath("/resources/product[@name='QQ']/account[@id='987654321']/password");
List<Element> list = xPath.selectNodes(document.getRootElement());
for (Element element : list) {
System.out.println(element.getTextTrim());
}
}
void normalParse() {
// 解析每个节点
List<Element> products = document.getRootElement().selectNodes("/resources/product");
Iterator<Element> iterator = products.iterator();
while (iterator.hasNext()) {
Element product = iterator.next();
// 获得属性
System.out.println(product.attributeValue("name"));
List<Element> accounts = product.selectNodes("account");
for (Element account : accounts) {
System.out.println("--" + account.attributeValue("id"));
// 获得值
System.out.println("-----" + account.elementTextTrim("password"));
}
}
}
void generate() {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("people");
Element p1 = root.addElement("p1");
p1.addAttribute("id", "12345");
p1.addElement("sex").setText("male");
p1.addElement("age").setText("18");
Element p2 = root.addElement("p2");
p2.addAttribute("id", "67890");
p2.addElement("sex").setText("female");
p2.addElement("age").setText("26");
XMLWriter xmlWriter = new XMLWriter();
try {
xmlWriter.setWriter(new FileWriter("resources/out.xml"));
xmlWriter.write(doc);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
xmlWriter.flush();
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
App app = new App();
app.xPathParse();
app.normalParse();
app.generate();
}
}
六、运行输出
456
QQ
--123456789
-----123
--987654321
-----456
Netease
--Tom
-----pwdOfTom
--Jim
-----pwdOfJim
七、生成文件
resources/out.xml
<?xml version="1.0" encoding="UTF-8"?>
<people>
<p1 id="12345">
<sex>male</sex>
<age>18</age>
</p1>
<p2 id="67890">
<sex>female</sex>
<age>26</age>
</p2>
</people>
八、小结
感觉java的代码都可以望文成义,看着代码和注释就能明白在干什么。打通xml之后,对各种框架里面用到的xml配置解析也更加熟悉一些,甚至可以自己写。