import java.io.ByteArrayInputStream;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XMLToMapUtil {

public static Map<String, String> xmlToMap(String xml, String charset)
throws UnsupportedEncodingException, DocumentException {

Map<String, String> respMap = new HashMap<String, String>();

SAXReader reader = new SAXReader();
Document doc = reader.read(new ByteArrayInputStream(xml.getBytes(charset)));
Element root = doc.getRootElement();
xmlToMap(root, respMap);
return respMap;
}

public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap) {

if (tmpElement.isTextOnly()) {
respMap.put(tmpElement.getName(), tmpElement.getText());
return respMap;
}

@SuppressWarnings("unchecked")
Iterator<Element> eItor = tmpElement.elementIterator();
while (eItor.hasNext()) {
Element element = eItor.next();
xmlToMap(element, respMap);
}
return respMap;
}
}

最新文章

  1. 记一次Redis被攻击的事件
  2. VSS 的修复和扫描
  3. Python 包管理工具解惑
  4. JAVA中保留指定小数位方法
  5. ELK 部署
  6. [原]poj-2488-water-DFS
  7. Linq 筛选出一条数据
  8. 5. openCV中常用函数学习
  9. Web 页面测试总结—控件类
  10. 用py2exe将python文件转换成exe可执行程序
  11. sqlplus命令历史解决方案
  12. 一起学Linux03之Linux系统目录结构
  13. 为啥程序会有bug?
  14. P3436 [POI2006]PRO-Professor Szu
  15. 高性能Mysql笔记 — 优化
  16. OSWatcher使用过程中小问题解决方法
  17. Spark的Streaming + Flume进行数据采集(flume主动推送或者Spark Stream主动拉取)
  18. Effective Java 第三版——88. 防御性地编写READOBJECT方法
  19. [SDOI2012]象棋
  20. git commit -m 与 git commit -am的区别

热门文章

  1. CentOS7的内核优化
  2. Docker使用Link与newwork在容器之间建立连接
  3. 初学者常用的LINUX命令
  4. Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
  5. 01 jmeter性能测试系列_Jmeter的体系结构
  6. mysql表关联
  7. 用JavaScript制作页面特效
  8. CSS:手机页面,常用字号和布局(工作中用)
  9. BATCH、事务、CLOB、BLOB
  10. SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介、创建消息生产者、创建消息消费者、自定义消息通道、分组与持久化、设置 RoutingKey)