Java中怎么将xml转换为Json

发布时间:2021-06-30 17:32:26 作者:Leah
来源:亿速云 阅读:351

这期内容当中小编将会给大家带来有关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了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. ASP.NET中XML转换为JSON,JSON转换为XML的方法
  2. 如何使用JDOM将Java对象转换为XML

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java json

上一篇:Feign的设计原理是什么

下一篇:一致性Hash的原理是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》