一 点睛

我们常常会遇到XML和Map的转换,在此提供一个工具类。

二 代码

package com.imooc.demo.common.util;

import org.apache.commons.lang3.StringUtils;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import java.util.*;

/**

* @className: XmlUtils

* @description: xml工具类

* @date: 2020/8/21

* @author: cakin

*/

public class XmlUtils {

/**

* 根据Map组装xml消息体,值对象仅支持基本数据类型、String、BigInteger、BigDecimal,以及包含元素为上述支持数据类型的Map

*

* @param vo map

* @param rootElement 根元素

* @return xml消息体

*/

public static String map2xmlBody(Map vo, String rootElement) {

org.dom4j.Document doc = DocumentHelper.createDocument();

Element body = DocumentHelper.createElement(rootElement);

doc.add(body);

__buildMap2xmlBody(body, vo);

return doc.asXML();

}

private static void __buildMap2xmlBody(Element body, Map vo) {

if (vo != null) {

Iterator it = vo.keySet().iterator();

while (it.hasNext()) {

String key = (String) it.next();

if (StringUtils.isNotEmpty(key)) {

Object obj = vo.get(key);

Element element = DocumentHelper.createElement(key);

if (obj != null) {

if (obj instanceof String) {

element.setText((String) obj);

} else {

if (obj instanceof Character || obj instanceof Boolean || obj instanceof Number

|| obj instanceof java.math.BigInteger || obj instanceof java.math.BigDecimal) {

org.dom4j.Attribute attr = DocumentHelper.createAttribute(element, "type", obj.getClass().getCanonicalName());

element.add(attr);

element.setText(String.valueOf(obj));

} else if (obj instanceof Map) {

org.dom4j.Attribute attr = DocumentHelper.createAttribute(element, "type", Map.class.getCanonicalName());

element.add(attr);

__buildMap2xmlBody(element, (Map) obj);

} else {

}

}

}

body.add(element);

}

}

}

}

/**

* 根据xml消息体转化为Map

*

* @param xml xml消息

* @param rootElement 根元素

* @return Map

* @throws DocumentException 文档异常

*/

public static Map xmlBody2map(String xml, String rootElement) throws DocumentException {

org.dom4j.Document doc = DocumentHelper.parseText(xml);

Element body = (Element) doc.selectSingleNode("/" + rootElement);

Map vo = __buildXmlBody2map(body);

return vo;

}

private static Map __buildXmlBody2map(Element body) {

Map vo = new HashMap<>();

if (body != null) {

List elements = body.elements();

for (Element element : elements) {

String key = element.getName();

if (StringUtils.isNotEmpty(key)) {

String type = element.attributeValue("type", "java.lang.String");

String text = element.getText().trim();

Object value = null;

if (String.class.getCanonicalName().equals(type)) {

value = text;

} else if (Character.class.getCanonicalName().equals(type)) {

value = new Character(text.charAt(0));

} else if (Boolean.class.getCanonicalName().equals(type)) {

value = new Boolean(text);

} else if (Short.class.getCanonicalName().equals(type)) {

value = Short.parseShort(text);

} else if (Integer.class.getCanonicalName().equals(type)) {

value = Integer.parseInt(text);

} else if (Long.class.getCanonicalName().equals(type)) {

value = Long.parseLong(text);

} else if (Float.class.getCanonicalName().equals(type)) {

value = Float.parseFloat(text);

} else if (Double.class.getCanonicalName().equals(type)) {

value = Double.parseDouble(text);

} else if (java.math.BigInteger.class.getCanonicalName().equals(type)) {

value = new java.math.BigInteger(text);

} else if (java.math.BigDecimal.class.getCanonicalName().equals(type)) {

value = new java.math.BigDecimal(text);

} else if (Map.class.getCanonicalName().equals(type)) {

value = __buildXmlBody2map(element);

} else {

}

vo.put(key, value);

}

}

}

return vo;

}

public static void main(String[] args) throws DocumentException {

Map vo = new HashMap();

vo.put("name", "1");

vo.put("age", 2);

Map vo3 = new HashMap();

vo3.put("name", "31");

vo3.put("age", 32);

vo.put("vo3", vo3);

String xmlBody = map2xmlBody(vo, "root");

System.out.println(xmlBody);

System.out.println("-----------------------------------------------------------");

Map vo2 = new HashMap();

vo2 = xmlBody2map(xmlBody, "root");

System.out.println(vo2.get("name"));

System.out.println(vo2.get("age"));

}

}

三 测试

131322

-----------------------------------------------------------

1

2

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐