import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* xml转换工具
*
* @Title XmlUtil.java
* @Description xml转换工具
* @Author wangyan
* @Date 2019年4月15日 下午4:12:16
*/
public class XmlUtil {
private static Logger logger = LoggerFactory.getLogger(XmlUtil.class);
private static final String PREFIX_XML = "<xml>";
private static final String SUFFIX_XML = "</xml>";
private static final String PREFIX_CDATA = "<![CDATA[";
private static final String SUFFIX_CDATA = "]]>";
/**
* 转化成xml, 单层无嵌套
*
* @param map
* @param isAddCDATA
* @return
*/
/**
* 转化成xml, 单层无嵌套
*
* @Title mapToXml 转化成xml, 单层无嵌套
* @Description
* @author wangyan
* @Date 2019年4月15日 下午4:00:42
* @param param
* @param isAddCDATA
* @return
* @version
*/
public static String mapToXml(Map<String, Object> param, boolean isAddCDATA) {
String result = "";
if (param != null) {
StringBuffer strbuff = new StringBuffer(PREFIX_XML);
for (Entry<String, Object> entry : param.entrySet()) {
strbuff.append("<").append(entry.getKey()).append(">");
if (isAddCDATA) {
strbuff.append(PREFIX_CDATA);
if (null != entry.getValue()) {
strbuff.append(entry.getValue());
}
strbuff.append(SUFFIX_CDATA);
} else {
if (null != entry.getValue()) {
strbuff.append(entry.getValue());
}
}
strbuff.append("</").append(entry.getKey()).append(">");
}
result = strbuff.append(SUFFIX_XML).toString();
}
return result;
}
/**
* 解析xml
*
* @Title xmlToMap 解析xml
* @Description
* @author wangyan
* @Date 2019年4月15日 下午3:39:25
* @param result 需要解析的xml字符串
* @return map
* @throws Exception
* @version
*/
public static Map<String, Object> xmlToMap(String result) {
// 将解析结果存储在HashMap中
Map<String, Object> map = new HashMap<String, Object>();
try {
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(new ByteArrayInputStream(result.getBytes()));
// 得到xml根元素
Element root = document.getRootElement();
// 进行迭代;读取根节点下的所有节点和子节点下的所有节点
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
if (element.elements() != null && element.elements().size() > 0) {
Map<String, String> tempMap = new HashMap<String, String>();
for (Iterator j = element.elementIterator(); j.hasNext();) {
Element temp = ((Element) j.next());
tempMap.put(temp.getName(), temp.getTextTrim());
}
map.put(element.getName(), tempMap);
} else {
map.put(element.getName(), element.getTextTrim());
}
}
} catch (Exception e) {
logger.error("【MessageUtil-parseXml失败:", e);
}
return map;
}
/**
* 测试
*
* @Title main
* @Description
* @author wangyan
* @Date 2019年4月15日 下午4:12:06
* @param args
* @version
*/
public static void main(String[] args) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("version", "1");
param.put("billno", "20190415");
param.put("orderinfono", "20190419");
param.put("orderinfoname", "wangxiaoer");
param.put("feeitemid", "1");
param.put("returnURL", "/test");
param.put("billremark", "aaaaa");
param.put("feeord", "001");
param.put("amt", 1.20);
param.put("dtlremark", "testaaaaa");
String result = mapToXml(param, true);
System.out.println(result);
Map<String, Object> tt = xmlToMap(result);
System.out.println(tt);
}
}
参考网址: