0

I have to convert xml code to Json. I have tried to use this code:

public String getJSON(String toConvert) {

    String jsonString = null;
    try {
        JSONObject xmlJSONObj = XML.toJSONObject(toConvert);
        jsonString = xmlJSONObj.toString();

    } catch (JSONException je) {
        System.out.println(je.toString());
    }
    return jsonString;
}

It works, but only for a short String. What should I do to make it work with a very long String?

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • 1
    So, what is wrong when there is a long string? Exception is thrown? No output? Undesired behaviour? – Manuel S. Mar 24 '16 at 13:23
  • http://i67.tinypic.com/or2iog.jpg – Filomena Del Sorbo Mar 24 '16 at 13:38
  • And where are you getting toConvert String from? Could you please paste the full code? – Manuel S. Mar 24 '16 at 13:44
  • public class Main { public static void main(String[] args) { String TEST_XML_STRING = ""; Convertitore converter=new Convertitore(); System.out.println(converter.getJSON(TEST_XML_STRING)); } } the string TEXT_XML_STRING is a string of 2000 lines that i cannot write.. – Filomena Del Sorbo Mar 24 '16 at 13:49

1 Answers1

0

It is not problem with convert xml to json. That part (method getJson) is correct. constant string too long java compile error is the problem. You can resolve it, look at that question and answers.

Community
  • 1
  • 1
WGawel
  • 72
  • 6
  • so you think that i have to use a file? – Filomena Del Sorbo Mar 24 '16 at 14:11
  • The best choice is to use a file (eg. shortes method: `String content = new String(Files.readAllBytes(Paths.get("filename.txt"))` ([source](http://www.adam-bien.com/roller/abien/entry/java_8_reading_a_file)). – WGawel Mar 24 '16 at 14:22