json转xml,xml转json

先添加maven依赖

        <dependency>
            <groupId>com.oracle.ojdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.8</version>
        </dependency>

然后代码示例

package com.example.demo.demo;


import cn.hutool.json.XML;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class test {

    public static void main(String[] args) {
        String xml = "<xml><a>a</a><b>b</b></xml>";
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(XML.toJSONObject(xml)));
        System.out.println("xml转json:"+jsonObject);
        String s = XML.toXml(jsonObject);
        System.out.println("json转xml:"+s);
    }
}

远行结果

推荐Hutool的API文档里面有很多实用的接口 https://www.hutool.cn/

例如我里面用的

发布了11 篇原创文章 · 获赞 6 · 访问量 453

猜你喜欢

转载自blog.csdn.net/weixin_45993202/article/details/103937132
今日推荐