jav springboot XML转换工具类.

简介: jav springboot XML转换工具类.
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jeecg.weixin.common.error.WxRuntimeException;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultText;
import org.xml.sax.SAXException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * <pre>
 * XML转换工具类.
 * Created by Binary Wang on 2018/11/4.
 * </pre>
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
public class XmlUtils {
  public static Map<String, Object> xml2Map(String xmlString) {
    Map<String, Object> map = new HashMap<>(16);
    try {
      SAXReader saxReader = new SAXReader();
      saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
      saxReader.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
      saxReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
      saxReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
      saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
      Document doc = saxReader.read(new StringReader(xmlString));
      Element root = doc.getRootElement();
      List<Element> elements = root.elements();
      for (Element element : elements) {
        map.put(element.getName(), element2MapOrString(element));
      }
    } catch (DocumentException | SAXException e) {
      throw new WxRuntimeException(e);
    }
    return map;
  }
  private static Object element2MapOrString(Element element) {
    final List<Node> content = element.content();
    final Set<String> names = names(content);
    // 判断节点下有无非文本节点(非Text和CDATA),如无,直接取Text文本内容
    if (names.size() < 1) {
      return element.getText();
    }
    Map<String, Object> result = Maps.newHashMap();
    if (names.size() == 1) {
      // 说明是个列表,各个子对象是相同的name
      List<Object> list = Lists.newArrayList();
      for (Node node : content) {
        if (node instanceof DefaultText) {
          continue;
        }
        if (node instanceof Element) {
          list.add(element2MapOrString((Element) node));
        }
      }
      result.put(names.iterator().next(), list);
    } else {
      for (Node node : content) {
        if (node instanceof DefaultText) {
          continue;
        }
        if (node instanceof Element) {
          result.put(node.getName(), element2MapOrString((Element) node));
        }
      }
    }
    return result;
  }
  private static Set<String> names(List<Node> nodes) {
    Set<String> names = Sets.newHashSet();
    for (Node node : nodes) {
      // 如果节点类型是Text或CDATA跳过
      if (node instanceof DefaultText || node instanceof CDATA) {
        continue;
      }
      names.add(node.getName());
    }
    return names;
  }
}
相关文章
|
2月前
|
XML Java 测试技术
【SpringBoot】基于 Maven 的 pom.xml 配置详解
【SpringBoot】基于 Maven 的 pom.xml 配置详解
222 0
【SpringBoot】基于 Maven 的 pom.xml 配置详解
|
3月前
|
XML Java 数据库连接
Spring Boot整合Mybatis(注解版+XML版)
Spring Boot整合Mybatis(注解版+XML版)
52 0
|
8月前
|
XML Java 数据库连接
SpringBoot-19-Mybatis的xml配置方式
在上一章节中,我们已经简单介绍mybatis的增删改查的基本操作,基础(单表)的增删改查可以按照,如果稍微复杂一些我们就需要使用mybatis的xml格式去实现。 那么我们开始使用mybatis的xml方式去实现增删改查。
64 0
|
4月前
|
XML Java 数据库连接
SpringBoot - 整合MyBatis配置版(XML)并开启事务
SpringBoot - 整合MyBatis配置版(XML)并开启事务
113 0
|
4月前
|
XML SQL Java
springboot 项目启动报Has been loaded by XML or SqlProvider, ignoring the injection of the SQL的错误的解决方案
springboot 项目启动报Has been loaded by XML or SqlProvider, ignoring the injection of the SQL的错误的解决方案
163 0
|
4月前
|
Java
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
36 0
|
5月前
|
XML Java 数据库连接
Springboot 中同时使用mybatis注解和springbean-xml配置方式
因为自己新建了一个应用,为了开发的速度,直接选用了springboot,但后来发现大部分读库的代码和同事已有的代码重复, 索性直接拿过来用。但问题是我已有的代码是通过纯注解的方式使用mybatis,同事代码是spring+xml来使用mybatis,经过几天的探索,发现一种两种方式结合使用的方法。
49 0
|
8月前
|
XML Java 数据格式
解决SpringBoot获取mapper.xml路径的问题
当mapper.xml与mapper.class放在同一文件夹下时,是不能够将xml文件打包进项目的,项目构建的时候不会加载到target文件夹中。在pom.xml中加入如下这句:
185 0
|
8月前
|
Java Maven
打通Maven中Pom.xml与SpringBoot多环境配置
通过pom.xml配置profiles节点,以及自定义打包配置文件实现SpringBoot多环境文件配置
355 0
|
3天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
11 0