今日问题 小记之
工作中我们有时会遇到map转换xml的需求 今天特意记下
话不多说 直接上代码 使用递归的思路
public static String map2Xml(Map<String, Object> paramMap) {
synchronized (MapUtil.class) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("<?xml version='1.0' encoding='UTF-8' ?>\n");
strBuilder.append("<REQ_PARAM>");
Set<String> objSet = paramMap.keySet();
for (String key : objSet) {
if (key == null) {
continue;
}
strBuilder.append("\n");
strBuilder.append("<").append(key).append(">\n");
Object value = paramMap.get(key);
strBuilder.append(convert(value));
strBuilder.append("</").append(key).append(">");
}
strBuilder.append("\n</REQ_PARAM>");
return strBuilder.toString();
}
}
public static String convert(Map map) {
StringBuilder strBuilder = new StringBuilder();
for (Object o : map.keySet()) {
strBuilder.append("<").append(o).append(">");
strBuilder.append(convert(map.get(o)));
strBuilder.append("</").append(o).append(">\n");
}
return strBuilder.toString();
}
public static String convert(Collection<?> objects) {
StringBuilder strBuilder = new StringBuilder("\n");
for (Object obj : objects) {
strBuilder.append("<").append(obj).append(">");
strBuilder.append(convert(obj));
strBuilder.append("</").append(obj).append(">\n");
}
return strBuilder.toString();
}
/**
* 描述:递归进行转换
* Created by zjw on 2018-12-11 11:21:37
*
* @param object
* @return String
*/
public static String convert(Object object) {
if (object instanceof Map) {
return convert((Map)object);
}
if (object instanceof Collection) {
return convert((Collection<?>) object);
}
StringBuilder strBuilder = new StringBuilder();
if (isObject(object)) {
Class<?> clz = object.getClass();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String fieldName = field.getName();
Object value;
try {
value = field.get(object);
} catch (IllegalArgumentException | IllegalAccessException e) {
continue;
}
strBuilder.append("<").append(fieldName).append("\">");
if (isObject(value)) {
strBuilder.append(convert(value));
} else {
strBuilder.append(value.toString());
}
strBuilder.append("</").append(fieldName).append(">");
}
} else if (object == null) {
strBuilder.append("null");
} else {
strBuilder.append(object.toString());
}
return strBuilder.toString();
}
/**
* 描述:判断是否是对象
* Created by zjw on 2018-12-11 11:20:48
*
* @param obj
* @return boolean
*/
private static boolean isObject(Object obj) {
if (obj == null) return false;
if (obj instanceof String) return false;
if (obj instanceof Integer) return false;
if (obj instanceof Double) return false;
if (obj instanceof Float) return false;
if (obj instanceof Byte) return false;
if (obj instanceof Long) return false;
if (obj instanceof Character) return false;
if (obj instanceof Short) return false;
return !(obj instanceof Boolean);
}
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Map<String, Object> reqParamMap = new HashMap<>();
Map<String, Object> pubInfoMap = new HashMap<>();
Map<String, Object> busInfoMap = new HashMap<>();
reqParamMap.put("PUB_INFO", pubInfoMap);
reqParamMap.put("BUSI_INFO", busInfoMap);
pubInfoMap.put("REQ_TIME", sdf.format(new Date()));
pubInfoMap.put("CHANNLE_ID", "789");
pubInfoMap.put("REGION_ID", "123");
pubInfoMap.put("REQ_SERIAL_NO", "20181211");
pubInfoMap.put("PROCESS_CODE", "TokenCheck");
busInfoMap.put("TOKEN", "testToken");
// busInfoMap.put("list", Arrays.asList("abc", "def"));
System.out.println(GsonUtil.toJson(reqParamMap));
System.out.println(MapUtil.map2Xml(reqParamMap));
}