XML文件转换为Json (org.json)



我正在尝试将XML转换为Json。我发现这个例子很简单,几乎就像我想要的那样。但是,有没有办法从我的计算机加载XML文件,而不是直接从代码?我已经找到了一些替代方案,但我还是想坚持使用org。如果可能的话Json…

public static String TEST_XML_STRING = ("C:\results\results.xml");或者类似的东西?

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class Main {

public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<breakfast_menu>n"+
"<food>n"+
"<name>Belgian Waffles</name>n"+
"<price>$5.95</price>n"+
"<description>n"+
"Two of our famous Belgian Waffles with plenty of real maple syrupn"+
"</description>n"+
"<calories>650</calories>n"+
"</food>n"+
"<food>n"+
"<name>Strawberry Belgian Waffles</name>n"+
"<price>$7.95</price>n"+
"<description>n"+
"Light Belgian waffles covered with strawberries and whipped creamn"+
"</description>n"+
"<calories>900</calories>n"+
"</food>n"+
"</breakfast_menu>";

public static void main(String[] args) {
try {JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException e) {
    System.out.println(e.toString());
}

}
}

我已经进入了这个,但是在第20行给我错误

main线程异常java.lang.Error: Unresolved compilation problem: at main .main(main .java:20)

import java.io.File;
import java.io.FileInputStream;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class Main {
    File file = new File("teste.xml");
    FileInputStream fin = new FileInputStream(file);
    byte[] xmlData = new byte[(int) file.length()];
    fin.read(xmlData);
    fin.close();
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING = new String(xmlData, "UTF-8");

public static void main(String[] args) {
try {JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException e) {
    System.out.println(e.toString());
}

}
}

您可以使用FileInputStream并将文件内容放入byte数组并将其传递给String构造函数以从文件中读取内容。

File file = new File("yourdata.xml");
FileInputStream fin = new FileInputStream(file);
byte[] xmlData = new byte[(int) file.length()];
fin.read(xmlData);
fin.close();
String TEST_XML_STRING = new String(xmlData, "UTF-8");

注:另一种方法是打开BufferedReader并循环调用readLine()

更新:请使用下面的代码,过程代码必须在方法/构造函数/初始化块内。它们不能在类块中。

public class Main {
    public static int PRETTY_PRINT_INDENT_FACTOR = 4;
    public static String TEST_XML_STRING = null;
    public static void main(String[] args) throws IOException {
        File file = new File("teste.xml");
        FileInputStream fin = new FileInputStream(file);
        byte[] xmlData = new byte[(int) file.length()];
        fin.read(xmlData);
        fin.close();
        TEST_XML_STRING = new String(xmlData, "UTF-8");
        try {
            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
            String jsonPrettyPrintString = xmlJSONObj
                    .toString(PRETTY_PRINT_INDENT_FACTOR);
            System.out.println(jsonPrettyPrintString);
        } catch (JSONException e) {
            System.out.println(e.toString());
        }
    }
}

你可以使用java Reader来完成。示例

Reader xmlSource = new FileReader("path/to/file.xml");
JSONObject json = XML.toJSONObject(xmlSource);
System.out.println(json);

你也可以从url中读取…

URL url = new URL("http://xml");
Reader xmlSource = new BufferedReader(new InputStreamReader(url.openStream()));
JSONObject json = XML.toJSONObject(xmlSource);
System.out.println(json);

相关内容

最新更新