<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
</dependency>
demo1从文件中解析
下面是一个books.xml文件
<books>
<book author="天蚕土豆">
<name>斗破苍穹</name>
<price>86</price>
</book>
<book author="萧潜">
<name>缥缈之旅</name>
<price>92</price>
</book>
<book author="萧鼎">
<name>诛仙</name>
<price>98</price>
</book>
<book author="天下霸唱">
<name>鬼吹灯</name>
<price>124</price>
</book>
<book author="辰东">
<name>神墓</name>
<price>128</price>
</book>
</books>
public class Dom4J_01 {
public static void main(String[] args) throws DocumentException {
//1.将xml文件加载到内存中
SAXReader saxReader = new SAXReader();
//document 加载执行xml文档获取Document对象
Document document = saxReader.read("books.xml");
//2.获取根元素的对象 -- books getRootElement 获取根元素
Element rootElement = document.getRootElement();
//每个元素的对象 都是Element对象
List<Element> elements = rootElement.elements();
//3.使用根元素的对象 对其他元素操作操作
//3.1获取所有的子元素对象 book
for (Element book : elements) {
//在循环内拿到了 每个 book 元素对象
//3.2获取book的属性值
String author = book.attributeValue("author");
//3.3获取book的子元素 name price
String name = book.element("name").getText();
String price = book.element("price").getText();
System.out.println(name+" "+author +" "+price );
}
}
}
斗破苍穹 天蚕土豆 86
缥缈之旅 萧潜 92
诛仙 萧鼎 98
鬼吹灯 天下霸唱 124
神墓 辰东 128
demo2也是从文件中读取
servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<servlets>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
</servlets>
public class Dom4J_02{
public static void main(String[] args) throws Exception {
// 1. 创建一个 saxReader 对象
SAXReader saxReader = new SAXReader();
// 2. 将数据读取到 document 对象中
Document document = saxReader.read("servlet.xml");
// 3. 使用 document 对象调用 getRootElement 方法获取根标签, 返回 Element 接口实现类对象
Element rootElement = document.getRootElement();
// 4. 使用 rootElement 根标签对象调用 elements 方法, 传入 servlet, 获取servlet标签对象
List<Element> servletElements = rootElement.elements("servlet");
// 5. 遍历, 并获取该标签下的子标签数据内容, 使用父标签对象调用elementText方法, 传入子标签名称获取数据
for (Element servlet : servletElements) {
String name = servlet.elementText("servlet-name");
String cls = servlet.elementText("servlet-class");
System.out.println(name + " : " + cls);
}
// 使用 rootElement 根标签对象调用 elements 方法, 传入 servlet-mapping, 获取servlet-mapping标签对象
List<Element> mappingElements = rootElement.elements("servlet-mapping");
for (Element mapping : mappingElements) {
String name = mapping.elementText("servlet-name");
String url = mapping.elementText("url-pattern");
System.out.println(name + " = " + url);
}
}
}
demo3字符串转成Document对象
//先加入dom4j.jar包
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* @Title: TestDom4j.java
* @Package
* @Description: 解析xml字符串
* @author 无处不在
* @date 2012-11-20 下午05:14:05
* @version V1.0
*/
public class TestDom4j {
public void readStringXml(String xml) {
Document doc = null;
try {
// 读取并解析XML文档
// SAXReader就是一个管道,用一个流的方式,把xml文件读出来
//
// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档
// Document document = reader.read(new File("User.hbm.xml"));
// 下面的是通过解析xml字符串的
doc = DocumentHelper.parseText(xml); // 将字符串转为XML
Element rootElt = doc.getRootElement(); // 获取根节点
System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head,这里的head就是上面打印出来的根节点的名称
// 遍历head节点
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值,这里同样也是需要head下存在这个title节点
System.out.println("title:" + title);
Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script
// 遍历Header节点下的Response节点
while (iters.hasNext()) {
Element itemEle = (Element) iters.next();
String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值
String password = itemEle.elementTextTrim("password");
System.out.println("username:" + username);
System.out.println("password:" + password);
}
}
Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body,这里看报文,有的报文可能叫Body大写字段开头
// 遍历body节点
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值
System.out.println("result:" + result);
Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form
// 遍历Header节点下的Response节点
while (itersElIterator.hasNext()) {
Element itemEle = (Element) itersElIterator.next();
String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值
String subID = itemEle.elementTextTrim("subID");
System.out.println("banlce:" + banlce);
System.out.println("subID:" + subID);
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description 将xml字符串转换成map
* @param xml
* @return Map
*/
public static Map readStringXmlOut(String xml) {
Map map = new HashMap();
Document doc = null;
try {
// 将字符串转为XML
doc = DocumentHelper.parseText(xml);
// 获取根节点
Element rootElt = doc.getRootElement();
// 拿到根节点的名称
System.out.println("根节点:" + rootElt.getName());
// 获取根节点下的子节点head
Iterator iter = rootElt.elementIterator("head");
// 遍历head节点
while (iter.hasNext()) {
Element recordEle = (Element) iter.next();
// 拿到head节点下的子节点title值
String title = recordEle.elementTextTrim("title");
System.out.println("title:" + title);
map.put("title", title);
// 获取子节点head下的子节点script
Iterator iters = recordEle.elementIterator("script");
// 遍历Header节点下的Response节点
while (iters.hasNext()) {
Element itemEle = (Element) iters.next();
// 拿到head下的子节点script下的字节点username的值
String username = itemEle.elementTextTrim("username");
String password = itemEle.elementTextTrim("password");
System.out.println("username:" + username);
System.out.println("password:" + password);
map.put("username", username);
map.put("password", password);
}
}
//获取根节点下的子节点body
Iterator iterss = rootElt.elementIterator("body");
// 遍历body节点
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
// 拿到body节点下的子节点result值
String result = recordEless.elementTextTrim("result");
System.out.println("result:" + result);
// 获取子节点body下的子节点form
Iterator itersElIterator = recordEless.elementIterator("form");
// 遍历Header节点下的Response节点
while (itersElIterator.hasNext()) {
Element itemEle = (Element) itersElIterator.next();
// 拿到body下的子节点form下的字节点banlce的值
String banlce = itemEle.elementTextTrim("banlce");
String subID = itemEle.elementTextTrim("subID");
System.out.println("banlce:" + banlce);
System.out.println("subID:" + subID);
map.put("result", result);
map.put("banlce", banlce);
map.put("subID", subID);
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static void main(String[] args) {
// 下面是需要解析的xml字符串例子
String xmlString = "" + "" + "dom4j解析一个例子 "
+ "" + ""
+ "" + "0 " + "
+ "1000 " + "36242519880716 "
+ "" + "" + "";
/*
* Test2 test = new Test2(); test.readStringXml(xmlString);
*/
Map map = readStringXmlOut(xmlString);
Iterator iters = map.keySet().iterator();
while (iters.hasNext()) {
String key = iters.next().toString(); // 拿到键
String val = map.get(key).toString(); // 拿到值
System.out.println(key + "=" + val);
}
}
}