资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java中怎么将xml转换为Json

这期内容当中小编将会给大家带来有关Java中怎么将xml转换为Json,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了富川免费建站欢迎大家使用!

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测试

 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");    }

控制台输出

{  "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

上述就是小编为大家分享的Java中怎么将xml转换为Json了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


当前题目:Java中怎么将xml转换为Json
本文来源:http://cdkjz.cn/article/ipisdd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220