【问题标题】:XML file to Json (org.json)XML 文件到 Json (org.json)
【发布时间】:2015-09-24 18:59:43
【问题描述】:

我正在尝试将 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 syrup\n"+
"</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 cream\n"+
"</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 中的异常:未解决的编译问题:在 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());
}


}
}

【问题讨论】:

  • 这个问题其实和Json/Xml没什么关系,而是在java中读取文件。也许您应该更改标题和描述以反映实际问题。
  • 我正在尝试使用另一个插件,但仍然出现错误。 stackoverflow.com/questions/32829289/…

标签: java json xml org.json


【解决方案1】:

您可以使用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());
        }

    }
}

【讨论】:

  • fin.read(xmlData); fin.close();给我错误:语法和 VariableDeclaratorId
  • 请在此处粘贴堆栈跟踪。
  • 线程“main”java.lang.Error 中的异常:未解决的编译问题:在 Main.main(Main.java:20)
  • @AdelM 似乎在名为 Main 的 Java 类的第 20 行存在编译错误。除非您粘贴整个堆栈跟踪,否则无法弄清楚它是什么。 Main 类的第 20 行还有什么语句?
  • 它的 public static void main(String[] args) {} 我将发布我的整个代码。它现在在我的主要帖子上
【解决方案2】:

您可以使用 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);

【讨论】:

    猜你喜欢
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多