最近在做国际客服北京职场的项目,需要提供一个接口服务端的能力,也就是需要开发一个http+xml的协议,入参和出参均为Map格式,各系统间的请求或应答是以xml格式封装的。在将返回报文(xml)解析为Map输出时遇到一个难点:Java对于多层级xml的解析。现以一个客户资料查询接口为例将解析过程记录如下:
返回xml报文的简化形式:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<HEAD>
<ORIGIN_DOMAIN>kefuxitongbianma</ORIGIN_DOMAIN>
<HOME_DOMAIN>CUGCRM</HOME_DOMAIN>
<ACTION_CODE>1</ACTION_CODE>
<BUSI_CODE>QUERYCUST</BUSI_CODE>
<TRANS_ID>20160220160635123456</TRANS_ID>
<RET_CODE>0000</RET_CODE>
<RET_MSG>success</RET_MSG>
</HEAD>
<BODY>
<TOTAL_RECORDS>20</TOTAL_RECORDS>
<TOTAL_PAGE>10</TOTAL_PAGE>
<CURRENT_PAGE>1</CURRENT_PAGE>
<CUSTINFOLIST>
<CUSTINFO>
<CUST_TYPE>001</CUST_TYPE>
<VIP_FLAG>true</VIP_FLAG>
</CUSTINFO>
<CUSTINFO>
<CUST_TYPE>002</CUST_TYPE>
<VIP_FLAG>false</VIP_FLAG>
</CUSTINFO>
<CUSTINFO>
<CUST_TYPE>003</CUST_TYPE>
<VIP_FLAG>false</VIP_FLAG>
</CUSTINFO>
</CUSTINFOLIST>
</BODY>
</ROOT>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
/**
* 解析xml的工具类
* 1、将多层级xml解析为Map
* 2、将多层级xml解析为Json
*
* @author lmb
*
*/
public class ParseXmlUtil {
public static Logger logger = Logger.getLogger(ParseXmlUtil.class);
public static void main(String[] args) {
// 获取一个xml文件
String textFromFile = MyXmlUtil.XmlToString();
//将xml解析为Map
Map resultMap = xml2map(textFromFile);
//将xml解析为Json
String resultJson = xml2Json(textFromFile);
}
/**
* 将xml格式响应报文解析为Json格式
* @param responseXmlTemp
* @return
*/
public static String xml2Json(String responseXmlTemp) {
Document doc = null;
try {
doc = DocumentHelper.parseText(responseXmlTemp);
} catch (DocumentException e) {
logger.error("parse text error : " + e);
}
Element rootElement = doc.getRootElement();
Map<String,Object> mapXml = new HashMap<String,Object>();
element2Map(mapXml,rootElement);
String jsonXml = JSONObject.fromObject(mapXml).toString();
System.out.println("Json >>> " + jsonXml);
return jsonXml;
}
/**
* 将xml格式响应报文解析为Map格式
* @param responseXmlTemp
* @param thirdXmlServiceBean
* @return
* @throws DocumentException
*/
public static Map<String, Object> xml2map(String responseXmlTemp) {
Document doc = null;
try {
doc = DocumentHelper.parseText(responseXmlTemp);
} catch (DocumentException e) {
logger.error("parse text error : " + e);
}
Element rootElement = doc.getRootElement();
Map<String,Object> mapXml = new HashMap<String,Object>();
element2Map(mapXml,rootElement);
System.out.println("Map >>> " + mapXml);
return mapXml;
}
/**
* 使用递归调用将多层级xml转为map
* @param map
* @param rootElement
*/
public static void element2Map(Map<String, Object> map, Element rootElement) {
//获得当前节点的子节点
List<Element> elements = rootElement.elements();
if (elements.size() == 0) {
//没有子节点说明当前节点是叶子节点,直接取值
map.put(rootElement.getName(),rootElement.getText());
}else if (elements.size() == 1) {
//只有一个子节点说明不用考虑list的情况,继续递归
Map<String,Object> tempMap = new HashMap<String,Object>();
element2Map(tempMap,elements.get(0));
map.put(rootElement.getName(),tempMap);
}else {
//多个子节点的话就要考虑list的情况了,特别是当多个子节点有名称相同的字段时
Map<String,Object> tempMap = new HashMap<String,Object>();
for (Element element : elements) {
tempMap.put(element.getName(),null);
}
Set<String> keySet = tempMap.keySet();
for (String string : keySet) {
Namespace namespace = elements.get(0).getNamespace();
List<Element> sameElements = rootElement.elements(new QName(string,namespace));
//如果同名的数目大于1则表示要构建list
if (sameElements.size() > 1) {
List<Map> list = new ArrayList<Map>();
for(Element element : sameElements){
Map<String,Object> sameTempMap = new HashMap<String,Object>();
element2Map(sameTempMap,element);
list.add(sameTempMap);
}
map.put(string,list);
}else {
//同名的数量不大于1直接递归
Map<String,Object> sameTempMap = new HashMap<String,Object>();
element2Map(sameTempMap,sameElements.get(0));
map.put(string,sameTempMap);
}
}
}
}
}
xml文件读取工具类:
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* 读取一个xml文件返回string
* @author lmb
*
*/
public class MyXmlUtil {
/**
* 加载xml文件
* @return Document
*/
public static Document load(){
Document document=null;
String url="E://2.xml";
try {
SAXBuilder reader = new SAXBuilder();
document=reader.build(new File(url));
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
/**
* 将xml文件转换为String串
* @return
*/
public static String XmlToString(){
Document document=null;
document=load();
Format format =Format.getPrettyFormat();
format.setEncoding("UTF-8");//设置编码格式
StringWriter out=null; //输出对象
String sReturn =""; //输出字符串
XMLOutputter outputter =new XMLOutputter();
out=new StringWriter();
try {
outputter.output(document,out);
} catch (IOException e) {
e.printStackTrace();
}
sReturn=out.toString();
return sReturn;
}
}
控制台打印结果:
Map >>> {BODY={TOTAL_RECORDS={TOTAL_RECORDS=20}, CUSTINFOLIST={CUSTINFO=[{CUST_TYPE={CUST_TYPE=001}, VIP_FLAG={VIP_FLAG=true}}, {CUST_TYPE={CUST_TYPE=002}, VIP_FLAG={VIP_FLAG=false}}, {CUST_TYPE={CUST_TYPE=003}, VIP_FLAG={VIP_FLAG=false}}]}, TOTAL_PAGE={TOTAL_PAGE=10}, CURRENT_PAGE={CURRENT_PAGE=1}}, HEAD={ACTION_CODE={ACTION_CODE=1}, ORIGIN_DOMAIN={ORIGIN_DOMAIN=kefuxit}, BUSI_CODE={BUSI_CODE=QUERYCUST}, HOME_DOMAIN={HOME_DOMAIN=CUGCRM}, TRANS_ID={TRANS_ID=20160220160635123456}, RET_MSG={RET_MSG=success}, RET_CODE={RET_CODE=0000}}}
Json >>> {"BODY":{"TOTAL_RECORDS":{"TOTAL_RECORDS":"20"},"CUSTINFOLIST":{"CUSTINFO":[{"CUST_TYPE":{"CUST_TYPE":"001"},"VIP_FLAG":{"VIP_FLAG":"true"}},{"CUST_TYPE":{"CUST_TYPE":"002"},"VIP_FLAG":{"VIP_FLAG":"false"}},{"CUST_TYPE":{"CUST_TYPE":"003"},"VIP_FLAG":{"VIP_FLAG":"false"}}]},"TOTAL_PAGE":{"TOTAL_PAGE":"10"},"CURRENT_PAGE":{"CURRENT_PAGE":"1"}},"HEAD":{"ACTION_CODE":{"ACTION_CODE":"1"},"ORIGIN_DOMAIN":{"ORIGIN_DOMAIN":"kefuxit"},"BUSI_CODE":{"BUSI_CODE":"QUERYCUST"},"HOME_DOMAIN":{"HOME_DOMAIN":"CUGCRM"},"TRANS_ID":{"TRANS_ID":"20160220160635123456"},"RET_MSG":{"RET_MSG":"success"},"RET_CODE":{"RET_CODE":"0000"}}}
格式化之后的结果如下:
map :
{
BODY={
TOTAL_RECORDS={TOTAL_RECORDS=20},
CUSTINFOLIST={
CUSTINFO=[
{
CUST_TYPE={CUST_TYPE=001},
VIP_FLAG={VIP_FLAG=true}
},
{
CUST_TYPE={CUST_TYPE=002},
VIP_FLAG={VIP_FLAG=false}
},
{
CUST_TYPE={CUST_TYPE=003},
VIP_FLAG={VIP_FLAG=false}
}
]
},
TOTAL_PAGE={TOTAL_PAGE=10},
CURRENT_PAGE={CURRENT_PAGE=1}
},
HEAD={
ACTION_CODE={ACTION_CODE=1},
ORIGIN_DOMAIN={ORIGIN_DOMAIN=kefuxit},
BUSI_CODE={BUSI_CODE=QUERYCUST},
HOME_DOMAIN={HOME_DOMAIN=CUGCRM},
TRANS_ID={TRANS_ID=20160220160635123456},
RET_MSG={RET_MSG=success},
RET_CODE={RET_CODE=0000}
}
}
Json:
{
"BODY":{
"TOTAL_RECORDS":{"TOTAL_RECORDS":"20"},
"CUSTINFOLIST":{
"CUSTINFO":[
{
"CUST_TYPE":{"CUST_TYPE":"001"},
"VIP_FLAG":{"VIP_FLAG":"true"}
},
{
"CUST_TYPE":{"CUST_TYPE":"002"},
"VIP_FLAG":{"VIP_FLAG":"false"}
},
{
"CUST_TYPE":{"CUST_TYPE":"003"},
"VIP_FLAG":{"VIP_FLAG":"false"}
}
]
},
"TOTAL_PAGE":{"TOTAL_PAGE":"10"},
"CURRENT_PAGE":{"CURRENT_PAGE":"1"}
},
"HEAD":{
"ACTION_CODE":{"ACTION_CODE":"1"},
"ORIGIN_DOMAIN":{"ORIGIN_DOMAIN":"kefuxit"},
"BUSI_CODE":{"BUSI_CODE":"QUERYCUST"},
"HOME_DOMAIN":{"HOME_DOMAIN":"CUGCRM"},
"TRANS_ID":{"TRANS_ID":"20160220160635123456"},
"RET_MSG":{"RET_MSG":"success"},
"RET_CODE":{"RET_CODE":"0000"}
}
}