有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用java将json转换为xml

我是java新手,所以我的java代码有问题。代码的结果是获得一个具有格式的xml文件

这是我的Java代码:

import java.io.*;
import com.github.tsohr.JSONArray;
import com.github.tsohr.JSONException;
import com.github.tsohr.JSONObject;
import com.github.tsohr.XML;
import java.nio.file.Files;
import java.nio.file.Paths;



    public class WriteToFile {       
public static void main(String[] args)throws Exception  {   
String path = new String(args[0]) ;             
String xmlStr =new String(Files.readAllBytes(Paths.get(path))("UTF-8"));
             System.out.println(xmlStr);
            JSONObject jsonObject = new JSONObject(xmlStr);
           String finalStr = XML.toString(jsonObject);
                System.out.println(finalStr);
                    System.out.println("done");         
                    FileWriter fw=new FileWriter(args[1]); 
                    fw.append("<JSON>");        
                    fw.write(finalStr);         
                    fw.append("</JSON>");           
                    fw.close(); } }

这就是我得到的,例如:

<car><blue>123</blue><car>

这就是我想要得到的:

<?xml version="1.0" encoding="UTF-8"?> 
  <car>    
     <blue>123</blue> 
  <car>

有人有解决办法吗

提前谢谢


共 (0) 个答案