【发布时间】:2018-08-31 13:53:38
【问题描述】:
我想从 xml 文件读取 JSONObject 并将其写入另一个 xml 文件。
但是我在某处将 JSONObject 写入 java 中的 xml 文件时出错了。
我正在使用 org.json jar 文件
请问我哪里出错了?
要求: 将 JSONObject 写入标签中的 XML 文件。
我的代码 sn-p:
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>
【问题讨论】: