dom4j读取xml
来源:互联网 发布:f100禁止mac访问 编辑:程序博客网 时间:2024/05/19 09:12
package com.lkx;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;public class Dom4jTest { /** * @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"); 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"); 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"); // 获取子节点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"); 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 = "<html>" + "<head>" + "<title>dom4j解析一个例子</title>"+ "<script>" + "<username>yangrong</username>"+ "<password>123456</password>" + "</script>" + "</head>"+ "<body>" + "<result>0</result>" + "<form>" + "<banlce>1000</banlce>" + "<subID>36242519880716</subID>"+ "</form>" + "<result>1</result>" + "<form>" + "<banlce>23000</banlce>" + "<subID>4324519880716</subID>"+ "</form>" + "</body>" + "</html>"; /* * 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); } } }
0 0
- [dom4j]dom4j读取XML小结
- Dom4J读取XML
- xml 读取 dom4j
- xml文件读取-dom4j
- Dom4j读取xml
- dom4j读取xml文件
- Dom4j读取xml
- dom4j读取xml
- dom4j读取xml文件
- dom4j读取,解析xml
- 使用DOM4J读取XML
- dom4j读取xml文件
- dom4j读取xml文档
- dom4j读取xml文档
- Dom4j读取XML文件
- java读取xml Dom4j
- Dom4j读取xml文件
- dom4j读取xml
- Android Studio修改虚拟机使用的内存大小
- Java语言概述
- use MFC in static LIB与use MFC in shared dll在下面这段程序的区别
- iOS各种设备信息获取总结
- Gradle for Android(三)多渠道打包、配置签名信息
- dom4j读取xml
- Winter Training #1
- C语言(24)------常用字符串处理函数大全
- day8:《Thinking in Java》笔记第九章---接口
- POJ 3295-Tautology(模拟-逻辑表达式)
- poj 1664 m个苹果放入n个盘子,递归写法和DP写法
- STL笔记介绍
- Live555 流媒体服务器启动
- Event处理(四)