将jsonobject数据写入xml标记 [英] write jsonobject data to xml tags

查看:94
本文介绍了将jsonobject数据写入xml标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从xml文件读取到JSONObject并将其写入另一个xml文件.

I want to read from an xml file to a JSONObject and write it to another xml file.

但是我在将JSONObject写入Java中的xml文件的地方出错了.

But I am going wrong somewhere to write JSONObject to xml file in java.

我正在使用org.json jar文件

I am using org.json jar file

我可以知道,我要去哪里了吗?

May I know, where am I going wrong?

要求: 将JSONObject写入标记中的XML文件.

Requirement : Write JSONObject to XML file in tags.

我的代码段:

public class xmltojson
{
    static String line="",str="";
    public static void main(String[] args) throws JSONException, IOException
    {
        String link = "SOURCE_FILEPATH\\Files.xml";
        BufferedReader br = new BufferedReader(new FileReader(link));
        while ((line = br.readLine()) != null)
        {
           str+=line;
        }

        JSONObject jsondata = XML.toJSONObject(str);

        String xml = XML.toString(jsondata);

        String xmlFile = DESTINATION_FILEPATH\\filefromjson.xml";

        try (FileWriter fileWriter = new FileWriter(xmlFile))
        {
            fileWriter.write(XML.toString(jsondata));
        }
    }
}

当前输出:

<?xml version="1.0"?>

-<Files>

<clsid>{215B2E53-57CE-475c-80FE-9EEC14635851}</clsid>

<disabled>0</disabled>


-<File>

    <clsid>{50BE44C8-567A-4ed1-B1D0-9234FE1F38AF}</clsid>

    <image>0</image>

    <uid>{18348506-E3DE-4C1E-A2DC-B91087376BC4}</uid>

    <name>default.jpeg</name>

    <disabled>0</disabled>


    -<Properties>

        <fromPath>C:\Users\Administrator\Documents\dragon_ball_kai-goku.jpeg</fromPath>

        <hidden>0</hidden>

        <action>C</action>

       <targetPath>C:\Users\Administrator\Downloads\default.jpeg</targetPath>

       <readOnly>0</readOnly>

       <archive>1</archive>

    </Properties>

    <status>default.jpeg</status>

    <changed>2018-08-14 06:36:56</changed>

</File>


....

但是预期输出是:

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

-<Files disabled="0" clsid="{215B2E53-57CE-475c-80FE-9EEC14635851}">


    -<File disabled="0" clsid="{50BE44C8-567A-4ed1-B1D0-9234FE1F38AF}" uid="{18348506-E3DE-4C1E-A2DC-B91087376BC4}" changed="2018-08-14 06:36:56" image="0" status="default.jpeg" name="default.jpeg">

        <Properties hidden="0" archive="1" readOnly="0" targetPath="C:\Users\Administrator\Downloads\default.jpeg" fromPath="C:\Users\Administrator\Documents\dragon_ball_kai-goku.jpeg" action="C"/>

</File>

推荐答案

通过查看XML.toJSONObject方法的文档:

将格式正确(但不一定有效)的XML字符串转换为JSONObject. 由于JSON是一种数据格式而XML是一种文档格式,因此在此转换中可能会丢失一些信息. XML使用元素,属性和内容文本,而JSON使用名称/值对和值数组的无序集合. JSON不喜欢在元素和属性之间进行区分.相似元素的序列表示为JSONArrays.内容文本可以放置在内容"成员中.注释,序言,DTD和< [[[]]>]将被忽略.

Convert a well-formed (but not necessarily valid) XML string into a JSONObject. Some information may be lost in this transformation because JSON is a data format and XML is a document format. XML uses elements, attributes, and content text, while JSON uses unordered collections of name/value pairs and arrays of values. JSON does not does not like to distinguish between elements and attributes. Sequences of similar elements are represented as JSONArrays. Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]> are ignored.

由于这种信息丢失,转换回的XML不等于原始XML.

Due to such imformation lost, the XML converted back is not equals to original XML.

要解决此问题,请改用JSONML.toJSONObject.作为方法的文档:

To solve the problem, use JSONML.toJSONObject instead. As the documentation of method:

使用JsonML转换将格式正确(但不一定有效)的XML字符串转换为JSONObject.每个XML标记都表示为具有"tagName"属性的JSONObject. 如果标记具有属性,则属性将在JSONObject中作为属性. 如果标签包含子项,则该对象将具有"childNodes"属性,该属性将是字符串和JsonML JSONObjects的数组.注释,序言,DTD和< [[[]]>]将被忽略.

Convert a well-formed (but not necessarily valid) XML string into a JSONObject using the JsonML transform. Each XML tag is represented as a JSONObject with a "tagName" property. If the tag has attributes, then the attributes will be in the JSONObject as properties. If the tag contains children, the object will have a "childNodes" property which will be an array of strings and JsonML JSONObjects. Comments, prologs, DTDs, and <[ [ ]]> are ignored.

网站提供了有关JsonML的更多详细信息.

This site provides more details about JsonML.

检查以下代码以查找不同的

Check the following code for the different

import org.json.JSONException;
import org.json.JSONML;
import org.json.JSONObject;
import org.json.XML;

public class JsonToXml {
    public static void main(String[] args) throws JSONException {

        String originalXML = "<?xml version=\"1.0\"?><a t=\"tt\"><b><c>1</c><c>2</c></b></a>";
        System.out.println("oringinal XML:" + originalXML);
        JSONObject jsonConvertByXML = XML.toJSONObject(originalXML);
        System.out.println("jsonConvertByXML:" + jsonConvertByXML);

        String xmlConvertedBackByXML = XML.toString(jsonConvertByXML);
        System.out.println("xmlConvertedBackByXML:" + xmlConvertedBackByXML);

        JSONObject jsonConvertByJSONML = JSONML.toJSONObject(originalXML);
        System.out.println(jsonConvertByJSONML);
        String xmlConvertedBackByJSONML = JSONML.toString(jsonConvertByJSONML);
        System.out.println("xmlConvertedBackByJSONML:" + xmlConvertedBackByJSONML);
    }
}

这篇关于将jsonobject数据写入xml标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆