java xml 转换 map_Java实现XML与Map转换工具类

一 点睛

我们常常会遇到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")); } }

三 测试

<?xml version="1.0" encoding="UTF-8"?> 131322 ----------------------------------------------------------- 1 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值