利用|利用 JDK 自带的 org.w3c.dom 进行对象, map 与 xml 的互相转换

【利用|利用 JDK 自带的 org.w3c.dom 进行对象, map 与 xml 的互相转换】2019独角兽企业重金招聘Python工程师标准>>> 利用|利用 JDK 自带的 org.w3c.dom 进行对象, map 与 xml 的互相转换
文章图片

利用 JDK 自带的 org.w3c.dom 进行对象,mapxml 的简单互相转换, 其中用到了一个工具类 Hutools 下面是hutoolsmaven依赖

cn.hutool hutool-all 4.1.19

下面是工具类
/** * xml 工具,基于 jdk 内置的 xml api * @author zcqshine * @date 2018/12/8 */ @Slf4j public class W3CXmlUtil { /** * Number 子类的类型名称集合 */ private static final Set NUMBER_CHILDREN_CLASS_NAME = new HashSet<>(); static { NUMBER_CHILDREN_CLASS_NAME.add(Integer.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(Float.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(Short.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(Double.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(Long.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(BigDecimal.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(BigInteger.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(int.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(short.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(float.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(double.class.getName()); NUMBER_CHILDREN_CLASS_NAME.add(long.class.getName()); }/** * xml 字符串转换为 Object * @param xmlStr * @param clazz * @param * @return * @throws Exception */ public static T readObjectFormXml(String xmlStr, Class clazz) throws Exception { T t; if (xmlStr == null || xmlStr.trim().length() == 0){ throw new IllegalArgumentException("XML content string is empty !"); } else { try { t = clazz.newInstance(); Document document = XmlUtil.parseXml(xmlStr); if (document != null){ Element element = document.getDocumentElement(); if (element != null){ NodeList nodeList = element.getChildNodes(); for (int i = 0; i(); if (StrUtil.isBlank(xmlStr)){ throw new IllegalArgumentException("xml is empty"); }else{ Document document = XmlUtil.parseXml(xmlStr); Element element = document.getDocumentElement(); if (element != null){ NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++){ Node node = nodeList.item(i); String nodeName = node.getNodeName(); String nodeText = node.getTextContent(); map.put(nodeName, nodeText); } } } return map; }/** * Object 转 xml 字符串 * @param t * @param * @return */ public static String object2Xml(T t){ try { //得到 DOM 解析器的工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //从 DOM 工厂中获得 DOM 解析器 DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("xml"); document.appendChild(root); Class clazz = t.getClass(); Field[] fields = clazz.getDeclaredFields(); if (fields.length > 0){ for (Field field : fields) { field.setAccessible(true); String fieldName = field.getName(); Element element = document.createElement(fieldName); Object obj = field.get(t); if (obj != null){ String value = https://www.it610.com/article/String.valueOf(obj); element.appendChild(document.createCDATASection(value)); } root.appendChild(element); } } return XmlUtil.toStr(document); } catch (ParserConfigurationException | IllegalAccessExceptione) { log.error("对象转 XML 失败. ", e); } return null; }/** * map 转 xml * @param map * @return */ public static String map2Xml(Map map){ if (map != null && !map.isEmpty()){ try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //从 DOM 工厂中获得 DOM 解析器 DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element root = document.createElement("xml"); document.appendChild(root); map.entrySet().forEach(item -> { String key = item.getKey(); T object = item.getValue(); String value = https://www.it610.com/article/null; if (object != null){ value = String.valueOf(object); } Element element = document.createElement(key); element.appendChild(document.createCDATASection(value)); root.appendChild(element); }); return XmlUtil.toStr(document); } catch (ParserConfigurationException e) { log.error("map转 xml 失败.", e); } } return null; } }

转载于:https://my.oschina.net/zcqshine/blog/2986663

    推荐阅读