Xml字符串转Map

前端之家收集整理的这篇文章主要介绍了Xml字符串转Map前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

说明:除head、body节点合并为一个Map,其余的都进行List<Map>转换;

比如XML:



则节点<tran>为一个Map,VALUE为List<Map>,其中<rcd>节点不显示名称,具体代码如下:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class XmlUtils {
private static final String HEAD = "head";
private static final String BODY = "body";


public static void main(String[] args) throws DocumentException {
String xml = "<ap>   <head>      <tr_code>330003</tr_code>      <corp_no>8000500436</corp_no>      <user_no>22222</user_no>      <req_no>123456789001</req_no>      <tr_acdt>20160510</tr_acdt>      <tr_time>151238</tr_time>      <atom_tr_count>1</atom_tr_count>      <channel>0</channel>      <reserved />   </head>   <body>      <cert_no>TEST01234</cert_no>      <pay_acno>310066865018010123060</pay_acno>      <type>S</type>      <sum>1</sum>      <sum_amt>0.01</sum_amt>      <pay_month>201605</pay_month>      <summary>测试0.01</summary>      <busi_no>3100012379</busi_no>      <selsec_flg>0</selsec_flg><tran><rcd><card_no>00000001</card_no><acname>测试账号</acname><card_flag>0</card_flag><amt>21.01</amt><busino>111111111</busino></rcd><rcd><card_no>00000001</card_no><acname>测试账号</acname><card_flag>0</card_flag><amt>21.01</amt><busino>111111111</busino></rcd></tran>   </body></ap>";
System.out.println(parseXmlStr(xml).toString());
}


/**
* 解析XML字符串
* 
* @param xml
* @return
* @throws DocumentException
*/
private static Map<String,Object> parseXmlStr(String xml)
throws DocumentException {
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
return parseElement(root);
}


/**
* 解析Element
* 
* @param root
* @return
*/
@SuppressWarnings("unchecked")
private static Map<String,Object> parseElement(Element root) {
String rootName = root.getName();
Iterator<Element> rootItor = root.elementIterator();
Map<String,Object> rMap = new HashMap<>();
List<Map<String,Object>> rList = new ArrayList<>();
Map<String,Object> rsltMap = null;
while (rootItor.hasNext()) {
Element tmpElement = rootItor.next();
String name = tmpElement.getName();
if (rsltMap == null || (!HEAD.equals(name) && !BODY.equals(name)
&& !tmpElement.isTextOnly())) {
if (!HEAD.equals(name) && !BODY.equals(name)
&& !tmpElement.isTextOnly() && rsltMap != null) {
rList.add(rsltMap);
}
rsltMap = new HashMap<>();
}
if (!tmpElement.isTextOnly()) {
Iterator<Element> headItor = tmpElement.elementIterator();
while (headItor.hasNext()) {
Element hElement = headItor.next();
if (hElement.isTextOnly()) {
rsltMap.put(hElement.getName(),hElement.getTextTrim());
} else {
rsltMap.putAll(parseElement(hElement));
}
}
}
}
rList.add(rsltMap);
rMap.put(rootName,rList);
return rMap;
}


}


测试结果:

{ap=[{summary=测试0.01,tr_time=151238,cert_no=TEST01234,channel=0,tr_code=330003,sum=1,type=S,pay_month=201605,req_no=123456789001,user_no=22222,tr_acdt=20160510,sum_amt=0.01,reserved=,atom_tr_count=1,pay_acno=310066865018010123060,busi_no=3100012379,corp_no=8000500436,selsec_flg=0,tran=[{acname=测试账号,card_no=00000001,card_flag=0,amt=21.01,busino=111111111},{acname=测试账号,busino=111111111}]}]}

原文链接:https://www.f2er.com/xml/295236.html

猜你在找的XML相关文章