简体   繁体   English

使用eclipse在java中将.xml文件转换为.json文件?

[英]Convert .xml file to .json file in java using eclipse?

This is my approach till now.这是我到目前为止的方法。

package com.xmltojson.parse_xml_to_json;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;

public class XMLtoJsonConverter 
{
    private URL url = null;
    private InputStream inputStream = null;

    public void getXMLfromJson() {
        try {
            url = XMLtoJsonConverter.class.getClassLoader().getResource("sampleParse.xml");
            inputStream = url.openStream();
            String xml = IOUtils.toString(inputStream);
            JSON objJson = new XMLSerializer().read(xml);


            System.out.println("JSON data : " + objJson);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                url = null;
            } catch (IOException ex) {
            }
        }
    }

    public static void main(String[] args) {
        new XMLtoJsonConverter().getXMLfromJson();
    }
}

Below is my sample xml file下面是我的示例 xml 文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<terms>

<term id="108822" SC="CO" LC="EN" LQ="UK" Type="DES">
        <label>soyabeans</label>
        <hn>From 1983.</hn>
        <uf idref="108843"/>
        <bt idref="52990"/>
        <cpl idref="52335"/>
        <cpl idref="52340"/>
</term>

    <term id="108843" SC="CO" LC="EN" LQ="US" Type="NPT">
        <label>soybeans</label>
        <use idref="108822"/>
    </term>

    <term id="52990" SC="CO" LC="EN" LQ="NA" Type="DES">
        <label>grain legumes</label>
        <uf idref="98121"/>
        <bt idref="52982"/>
    </term>

    <term id="52335" SC="ON" LC="EN" LQ="NA" Type="DES">
        <label>Glycine max</label>
        <com>bhatta (Hindi) (India);
        bhatta (Nepali) (India);
        bhatta (Nepali) (Nepal);
        bhattamash (Nepali) (India);
        bhattamash (Nepali) (Nepal);</com>
        <uf idref="108125"/>
        <uf idref="52333"/>
    </term>

    <term id="52340" SC="ON" LC="EN" LQ="NA" Type="DES">
        <label>Glycine soja</label>
        <uf idref="52341"/>
        <uf idref="52344"/>
        <bt idref="52327"/>
        <hpr idref="108822"/>
    </term>

</terms>

I want to convert this xml to below shown jsonFormat我想将此 xml 转换为下面显示的 jsonFormat

[
               {
                               "term": "soyabeans",
                               "termHelp": "uf: soybeans, bt: grain legumes",
               }
]

where term is label name and all other tags would come in termHElp.其中 term 是标签名称,所有其他标签都将出现在 termHElp 中。 Some of the term help element contain reference to other XML tag reference from where there label value is picked up and put it in with reference to termhelp item一些术语帮助元素包含对其他 XML 标记引用的引用,从那里获取标签值并将其放入引用 termhelp 项

I see your JSON representation is not straight transformation of your input XML, so you can't achieve what you are asking using any available standard open source libraries like Jackson, Gson, Json etc. You will have to do this in 3 steps:我看到您的 JSON 表示不是您输入 XML 的直接转换,因此您无法使用任何可用的标准开源库(如 Jackson、Gson、Json 等)来实现您的要求。您必须分 3 个步骤执行此操作:


1) Deserialize XML to POJO(say class XMLPOJO) using JAXB 1 ) 使用JAXB XML反序列化为 POJO(比如类 XMLPOJO)

2) Transform XMLPOJO to JSONPOJO (class representing desired JSON format) using standard Java code 2)使用standard Java代码将 XMLPOJO转换为 JSONPOJO(表示所需 JSON 格式的类)

3) Serialize POJO (class JSONPOJO) to Json using any JSON parser libraries like Jackson etc. 3)使用任何JSON parser库(如 Jackson 等)将 POJO(类 JSONPOJO )序列化为 Json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM