前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java xml转Json工具类

Java xml转Json工具类

作者头像
疯狂的KK
发布2020-02-26 09:33:45
2.9K0
发布2020-02-26 09:33:45
举报
文章被收录于专栏:Java项目实战Java项目实战

看了下CSDN博客,关于工具类的大多为下载需要C币,本工具类xml转成json的大部分xml格式都适用。

代码语言:javascript
复制
public class XmlToJsonUtils {
    private static Log log = LogFactory.getLog(Xml2JsonUtils.class);
    public static String xml2json(String xml) {
        //读取Xml文件
        StringReader input = new StringReader(xml);
        StringWriter output = new StringWriter();
        //开启自动配置
        JsonXMLConfig config = new JsonXMLConfigBuilder().autoArray(true).autoPrimitive(true).prettyPrint(true).build();
        try {
        //输入流事件
            XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(input);
            XMLEventWriter writer = new JsonXMLOutputFactory(config).createXMLEventWriter(output);
            writer.add(reader);
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       //json特殊格式  需要去除特殊符号,并保证源xml文件能符合要求 否则转换失败
        return output.toString().replace("@", "").replace("#", "").replace("\"{", "{").replace("}\"", "}").replace("\\", "");
      }
    }

demo测试

代码语言:javascript
复制
 public static void main(String[] args) throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String text = FileUtils.readFileToString(new File("F:\\pom.xml"), "UTF-8");
        System.out.println(Xml2JsonUtils.xmlTojson(text));
        stopWatch.stop();
        System.out.println("用时"+stopWatch.getTotalTimeMillis()+"ms");
    }

控制台输出

代码语言:javascript
复制
{
  "project" : {
    "xmlns" : "http://maven.apache.org/POM/4.0.0",
    "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance",
    "xsi:schemaLocation" : "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd",
    "modelVersion" : "4.0.0",
    "groupId" : "com.kk",
    "artifactId" : "kk",
    "version" : "0.0.1-SNAPSHOT",
    "packaging" : "war",
    "build" : {
      "plugins" : {
        "plugin" : {
          "groupId" : "org.apache.maven.plugins",
          "artifactId" : "maven-compiler-plugin",
          "version" : "3.5.1",
          "configuration" : {
            "source" : 1.8,
            "target" : 1.8,
            "showWarnings" : true
          }
        }
      },
      "resources" : {
        "resource" : [ {
          "directory" : "src/main/resources",
          "excludes" : {
            "exclude" : [ "**/*.properties", "**/*.xml" ]
          },
          "filtering" : false
        }, {
          "directory" : "src/main/java",
          "includes" : {
            "include" : [ "**/*.properties", "**/*.xml" ]
          },
          "filtering" : false
        }, {
          "directory" : "src/main/resources",
          "includes" : {
            "include" : [ "*.properties", "*.xml" ]
          },
          "filtering" : false
        }, {
          "directory" : "${project.basedir}/lib",
          "targetPath" : "WEB-INF/lib",
          "filtering" : true,
          "includes" : {
            "include" : "**/*.jar"
          }
        } ]
      }
    },
    "properties" : {
      "spring.version" : "4.3.3.RELEASE",
      "mybatis.version" : "3.4.0",
      "slf4j.version" : "1.7.7",
      "log4j.version" : "1.2.17"
    },
    "dependencies" : {
      "dependency" : [ {
        "groupId" : "aopalliance",
        "artifactId" : "aopalliance",
        "version" : 1.0
      }, {
        "groupId" : "de.odysseus.staxon",
        "artifactId" : "staxon",
        "version" : 1.3
      }, {
        "groupId" : "xom",
        "artifactId" : "xom",
        "version" : "1.2.5"
      },  {
        "groupId" : "org.springframework.security",
        "artifactId" : "spring-security-web",
        "version" : "4.1.3.RELEASE"
      }, {
        "groupId" : "org.springframework.security",
        "artifactId" : "spring-security-config",
        "version" : "4.1.3.RELEASE"
      }, {
        "groupId" : "redis.clients",
        "artifactId" : "jedis",
        "version" : "2.9.0"
      }, {
        "groupId" : "org.springframework.data",
        "artifactId" : "spring-data-redis",
        "version" : "1.8.7.RELEASE"
      }, {
        "groupId" : "org.springframework.session",
        "artifactId" : "spring-session",
        "version" : "1.3.1.RELEASE"
      }]
    }
  }
}
用时261ms

基本上大部分xml格式是支持的

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 赵KK日常技术记录 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档