xml转JSON

依赖

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>

代码

@Test
    public  void  test5(){
        try {
            InputStreamReader is = new InputStreamReader(new FileInputStream("D://Contory.xml"),"utf-8");//获取xml文件,并且转为utf-8格式
            String xml = IOUtils.toString(is);//把xml文件转换为String类型格式
            org.json.JSONObject json = XML.toJSONObject(xml);//使用toJSONObject转换成json格式
            System.out.println(json);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

xml文件内容

<World Conturys="世界上所有的国家" Des="所有的国家都在一个地球上">
    <Inst Instruct="本xml主要是描述国家信息。。。"></Inst>
    <Contury Names="China" CityNum="999" PublishDate="1949">
        <City Name="北京市" Des="中国的首都" People="330">标签之间的内容</City>
        <City Name="上海市" Des="中国的魔都" People="300"></City>
        <City Name="天津市" Des="中国的一个直辖市" People="100"></City>
        <City Name="湖南市" Des="湘潭老街" People="80" />
    </Contury>
    <Contury Names="USA">
        <City Name="华盛顿" Des="以华盛顿命名" People="130"></City>
        <City Name="德州" Des="德州扑克" People="140"></City>
    </Contury>
    <Contury Names="UK">
        <City Name="巴黎" Des="浪漫之都" People="80"></City>
    </Contury>
</World>

json数据

{"World":{"Des":"所有的国家都在一个地球上","Inst":{"Instruct":"本xml主要是描述国家信息。。。"},"Conturys":"世界上所有的国家","Contury":[{"Names":"China","CityNum":999,"PublishDate":1949,"City":[{"Des":"中国的首都","People":330,"content":"标签之间的内容","Name":"北京市"},{"Des":"中国的魔都","People":300,"Name":"上海市"},{"Des":"中国的一个直辖市","People":100,"Name":"天津市"},{"Des":"湘潭老街","People":80,"Name":"湖南市"}]},{"Names":"USA","City":[{"Des":"以华盛顿命名","People":130,"Name":"华盛顿"},{"Des":"德州扑克","People":140,"Name":"德州"}]},{"Names":"UK","City":{"Des":"浪漫之都","People":80,"Name":"巴黎"}}]}}

json解析之后

解析网址: https://www.json.cn/

{
    "World":{
        "Des":"所有的国家都在一个地球上",
        "Inst":{
            "Instruct":"本xml主要是描述国家信息。。。"
        },
        "Conturys":"世界上所有的国家",
        "Contury":[
            {
                "Names":"China",
                "CityNum":999,
                "PublishDate":1949,
                "City":[
                    {
                        "Des":"中国的首都",
                        "People":330,
                        "content":"标签之间的内容",
                        "Name":"北京市"
                    },
                    {
                        "Des":"中国的魔都",
                        "People":300,
                        "Name":"上海市"
                    },
                    {
                        "Des":"中国的一个直辖市",
                        "People":100,
                        "Name":"天津市"
                    },
                    {
                        "Des":"湘潭老街",
                        "People":80,
                        "Name":"湖南市"
                    }
                ]
            },
            {
                "Names":"USA",
                "City":[
                    {
                        "Des":"以华盛顿命名",
                        "People":130,
                        "Name":"华盛顿"
                    },
                    {
                        "Des":"德州扑克",
                        "People":140,
                        "Name":"德州"
                    }
                ]
            },
            {
                "Names":"UK",
                "City":{
                    "Des":"浪漫之都",
                    "People":80,
                    "Name":"巴黎"
                }
            }
        ]
    }
}