Search code examples
javajsonxmlorg.json

XML file to Json (org.json)


I'm trying to convert XML to Json. I found this example bellow and works almost like the way I wanted. But, is there any way to load the XML file from my computer and not direcly from the code? I've found some alternatives but I would like to stick with org.json if possible...

public static String TEST_XML_STRING = ("C:\\results\\results.xml"); or something like that?

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());
}


}
}

I've got into this, but gives me error on line 20

Exception in thread "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());
}


}
}

Solution

  • You can use FileInputStream and get the file content into a byte array and pass it to String constructor to read the content from file.

    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");
    

    NB: Another options is to open a BufferedReader and looping through calling readLine().

    Update: Please use the below code, Procedural codes must be inside method/constructor/initializer block. They cannot be inside a class block.

    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());
            }
    
        }
    }