Ficheiro XML para o Json (org.json)
Estou a tentar converter XML para Json.
Encontrei este exemplo e funciona quase como eu queria. Mas, há alguma maneira de carregar o ficheiro XML do meu computador e não directamente do Código? Eu encontrei algumas alternativas, mas eu gostaria de ficar com org.json, se possível...
public static String TEST_XML_STRING = ("C:\\results\\results.xml")
; ou algo assim?
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());
}
}
}
Eu meti-me nisto, mas dá-me um erro na linha 20.
excepção no tópico" main " java.idioma.Erro: problema de compilação não resolvido: at Principal.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());
}
}
}
0
1 answers
Você pode usar FileInputStream
e obter o conteúdo do arquivo em uma matriz byte
e passar para String
construtor para ler o conteúdo do arquivo.
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:outra opção é abrir um BufferedReader
e fazer looping através da chamada readLine()
.
Actualização: Por favor, use o código abaixo, os códigos processuais devem estar dentro do bloco método/construtor/inicializador. Não podem estar num bloco de aulas.
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());
}
}
}
1
Author: M4ver1k, 2015-09-25 12:51:41