当前位置:网站首页>xml转Map(递归调用读取XML全部节点内容) readXml 读取xml
xml转Map(递归调用读取XML全部节点内容) readXml 读取xml
2022-06-09 09:00:00 【tanzongbiao】
xml转Map(递归调用读取XML全部节点内容)
/**
* like12 add,20220601,递归调用读取XML全部节点内容
* xml转Map
* @param xmlStr
* @return
* @throws Exception
*/
public static Map<String, Object> xmlToMap(String xmlStr) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
//将xml格式的字符串转换成Document对象
Document doc = DocumentHelper.parseText(xmlStr);
//获取根节点
Element root = doc.getRootElement();
//递归调用
map = iteratorFunc(root, map);
return map;
}
/**
* 递归调用
* @param root
* @param map
* @return
*/
public static Map<String, Object> iteratorFunc(Element root, Map<String, Object> map) {
// 通过element对象的elementIterator方法获取迭代器
Iterator it = root.elementIterator();
// 遍历迭代器,获取根节点中的信息
while (it.hasNext()) {
Element book = (Element) it.next();
map.put(book.getName(), book.getTextTrim());
System.out.println(book.getName() + " " + book.getTextTrim());
//递归
map = iteratorFunc(book, map);
}
return map;
}读取XML readXml
// 读取XML
@SuppressWarnings({ "rawtypes" })
public Map<String, Object> readXml(String strIn) {
String retCode = null;
String retDesc = null;
String retValue = null;
// 创建SAXReader的对象reader
SAXReader reader = new SAXReader();
try {
//String转InputStream
ByteArrayInputStream byteArrayInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(
strIn.getBytes("GBK"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = (InputStream)byteArrayInputStream;
// 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
org.dom4j.Document document = reader.read(in);
//Document document = reader.read(new File("D://test.xml"));
// 通过document对象获取根节点bookstore
Element bookStore = document.getRootElement();
// 通过element对象的elementIterator方法获取迭代器
Iterator it = bookStore.elementIterator();
// 遍历迭代器,获取根节点中的信息(书籍)
while (it.hasNext()) {
//System.out.println("=====开始遍历某一本书=====");
Element book = (Element) it.next();
/*// 获取book的属性名以及 属性值
List<Attribute> bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println("属性名:" + attr.getName() + "--属性值:"
+ attr.getValue());
}*/
Iterator it2 = book.elementIterator();
while (it2.hasNext()) {
Element book2 = (Element) it2.next();
//System.out.println("节点名:" + book2.getName() + "--节点值:"
//+ book2.getStringValue());
if ("retCode".equals(book2.getName())) {
retCode = book2.getStringValue();
} else if ("retDesc".equals(book2.getName())) {
retDesc = book2.getStringValue();
} else if ("retValue".equals(book2.getName())) {
retValue = book2.getStringValue();
}
}
//System.out.println("=====结束遍历某一本书=====");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("retCode", retCode);
params.put("retDesc", retDesc);
params.put("retValue", retValue);
return params;
}边栏推荐
猜你喜欢
![[texstudio] [3] relatively complete paper typesetting template and bib file reference method](/img/ba/160b236aaf0fc905609b8a70c7677d.jpg)
[texstudio] [3] relatively complete paper typesetting template and bib file reference method

How to solve the problem of mouseup event invalidation

MySQL基础 函数篇

Remote prepayment management system helps property management solve the problem of difficult charging and statistics
![[antenna] [1] explanation of some nouns and simple concepts](/img/bb/49dc431a5302e3cea04e66f8545d02.jpg)
[antenna] [1] explanation of some nouns and simple concepts

【Redis学习12】分布式缓存之哨兵机制,分片集群

HVV blue team points north

环境变量Path误删除
![[texstudio] [2] general picture and table presentation](/img/1e/bb9488984cb1a3b233978d25f83b6e.png)
[texstudio] [2] general picture and table presentation

Editor description - alternate
随机推荐
Application of acrel bus intelligent lighting control system in hospital
Leetcode game 295
Configuring the environment for RMAN backups_ Configure backup retention policy
C language pointer
2022-2028 global online programming learning platform industry survey and trend analysis report
[antenna] [2] explanation of some nouns and simple concepts, still
FreeRTOS semaphore review
redhat7 破解(重置)root密码
了解图数据库neo4j(二)
Attachment 17: limited articles on network program interpretation
Web knowledge points 123
Can I LINQ a JSON- Can I LINQ a JSON?
RMAN backup concept_ Online backup and backup mode
Remote prepayment management system helps property management solve the problem of difficult charging and statistics
MySQL queries all database table names and their comments
项目面试题
Analysis methods of common problems in performance testing
MySQL基础 数据类型精讲
Linux在线安装一个Neo4j图数据库
2022-2028 global UAV detection and jamming system industry survey and trend analysis report