XML字符串转为Map
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;
}
}
最新文章
- 记一次Redis被攻击的事件
- VSS 的修复和扫描
- Python 包管理工具解惑
- JAVA中保留指定小数位方法
- ELK 部署
- [原]poj-2488-water-DFS
- Linq 筛选出一条数据
- 5. openCV中常用函数学习
- Web 页面测试总结—控件类
- 用py2exe将python文件转换成exe可执行程序
- sqlplus命令历史解决方案
- 一起学Linux03之Linux系统目录结构
- 为啥程序会有bug?
- P3436 [POI2006]PRO-Professor Szu
- 高性能Mysql笔记 — 优化
- OSWatcher使用过程中小问题解决方法
- Spark的Streaming + Flume进行数据采集(flume主动推送或者Spark Stream主动拉取)
- Effective Java 第三版——88. 防御性地编写READOBJECT方法
- [SDOI2012]象棋
- git commit -m 与 git commit -am的区别
热门文章
- CentOS7的内核优化
- Docker使用Link与newwork在容器之间建立连接
- 初学者常用的LINUX命令
- Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
- 01 jmeter性能测试系列_Jmeter的体系结构
- mysql表关联
- 用JavaScript制作页面特效
- CSS:手机页面,常用字号和布局(工作中用)
- BATCH、事务、CLOB、BLOB
- SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介、创建消息生产者、创建消息消费者、自定义消息通道、分组与持久化、设置 RoutingKey)