使用java将json转换为xml
我是java新手,所以我的java代码有问题。代码的结果是获得一个具有格式的xml文件 这是我的Java代码:使用java将json转换为xml,java,Java,我是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 ja
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(); } }
import java.io.*;
导入com.github.tsohr.JSONArray;
导入com.github.tsohr.JSONException;
导入com.github.tsohr.JSONObject;
导入com.github.tsohr.XML;
导入java.nio.file.Files;
导入java.nio.file.path;
公共类WriteToFile{
公共静态void main(字符串[]args)引发异常{
字符串路径=新字符串(args[0]);
String xmlStr=新字符串(Files.readAllBytes(path.get(path))(“UTF-8”);
System.out.println(xmlStr);
JSONObject JSONObject=新的JSONObject(xmlStr);
字符串finalStr=XML.toString(jsonObject);
系统输出打印LN(finalStr);
系统输出打印项次(“完成”);
FileWriter fw=新的FileWriter(args[1]);
fw.附加(“”);
fw.write(finalStr);
fw.附加(“”);
fw.close();}
这就是我得到的,例如:
<car><blue>123</blue><car>
123
这就是我想要得到的:
<?xml version="1.0" encoding="UTF-8"?>
<car>
<blue>123</blue>
<car>
123
有人有解决办法吗
提前谢谢 因此,您希望使用
\r\n
字符在单独的行中打印它,并获得描述?com.github.tsohr.XML
非常有限。改用JAXB或其他东西。@FailingCoder是的,我相信您可以向StringBuilder添加换行符。不过我不知道这个描述。