包含json文件轉換為xml文件的詞條

本文目錄一覽:

java解析json,然後將其轉換為xml

需要的依賴:

        dependency  

            groupIdde.odysseus.staxon/groupId  

            artifactIdstaxon/artifactId  

            version1.2/version  

        /dependency

代碼:

    public static void main(String[] args) throws Exception {

        // 輸入json文件

        String encoding = “UTF-8”;

        File jsonFile = new File(“C:/Users/Administrator/Desktop/person.json”);

        Long jsonFilelength = jsonFile.length();

        byte[] JsonBytes = new byte[jsonFilelength.intValue()];

        FileInputStream in = new FileInputStream(jsonFile);

        in.read(JsonBytes);

        in.close();

        String json = new String(JsonBytes, encoding);

        // json — xml

        StringReader input = new StringReader(json);

        StringWriter output = new StringWriter();

        JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false)

                .repairingNamespaces(false).build();

        XMLEventReader reader = new JsonXMLInputFactory(config)

                .createXMLEventReader(input);

        XMLEventWriter writer = XMLOutputFactory.newInstance()

                .createXMLEventWriter(output);

        writer = new PrettyXMLEventWriter(writer);

        writer.add(reader);

        reader.close();

        writer.close();

        output.close();

        input.close();

        // 輸出成xml文件

        File xmlFile = new File(“C:/Users/Administrator/Desktop/person.xml”);

        PrintWriter outFile = new PrintWriter(xmlFile);

        outFile.write(output.toString());

        outFile.flush();

        outFile.close();

    }

如何將json文件轉為xml格式的文件

json的話,目前比較流行的轉換工具是fastjson,使用方便,速度又很快。。可以在JavaBean和String型之間直接轉換

xml的話,可以採用XStream來轉換。。。

也就是說,你可以先把json的文件內容轉換成JavaBean(fastjson),然後再把JavaBean轉換成xml(XStream)

json如何轉換成xml

網上都有XML和json互相轉換的工具,在線就可以轉換。注意XML必須有單一的根節點。

?xml version=”1.0″ encoding=”UTF-8″ ?

root

node

id1/id

text系統維護/text

children

iduser/id

text人員維護/text

/children

children

idrole/id

text角色維護/text

iconClsicon-reload/iconCls

/children

/node

node

text開發維護/text

children

text元數據/text

stateclosed/state

children

text編輯樣式/text

/children

children

iddataview/id

text數據視圖/text

/children

/children

children

text權限資源/text

/children

/node

/root

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/219887.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 11:01
下一篇 2024-12-09 11:01

相關推薦

發表回復

登錄後才能評論