JSONObject.toString () throws OutOfMemoryError

I need to convert StackExchange dataset to Json file, so I tried this -

public class Parser {
    public static int PRETTY_FACTOR=4;
    public static void main(String[] args) throws Exception {  
        String fileName = "/home/dipannoy/Desktop/stackexchange/android/posthistory.json";
        String path= "/home/dipannoy/Desktop/stackexchange/android/PostHistory.xml";

        try {           

            StringBuilder builder =  new StringBuilder();  


            FileInputStream inputStream = null;
            Scanner sc = null;
            try {
                inputStream = new FileInputStream(path);
                sc = new Scanner(inputStream, "UTF-8");
                while (sc.hasNextLine()) {
                    String line = sc.nextLine();
                    builder.append(line);
                }
                if (sc.ioException() != null) {
                    throw sc.ioException();
                }
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (sc != null) {
                    sc.close();
                }
            }


            String xml  = builder.toString();  
            JSONObject jsonObj = XML.toJSONObject(xml);   


            FileWriter fileWriter = new FileWriter(fileName);
            BufferedWriter bufferedWriter =new BufferedWriter(fileWriter);

            bufferedWriter.write(jsonObj.toString());
            bufferedWriter.flush();            
            bufferedWriter.close();
            fileWriter.close();
        }


          catch(IOException ex) {
                System.out.println(
                    "Error writing to file '"
                    + fileName + "'");

            } catch(Exception e) {  
                e.printStackTrace();  
            }
    }  
}

      

But he had a mistake at jsonObj.toString()

. Sample xml -

<comments>
<row Id="1" PostId="1" Score="4" Text="Did I just place the first upvote?  Congrats on getting this site off the ground!" CreationDate="2016-01-12T18:47:12.573" UserId="23"/>
</comments>

      

I tried to use Gson but was unable to convert GSONObject

to GSONObject

as I GsonParser

must have a method GSONObject

toString()

that creates OutOfMemoryError

. Can anyone help with this issue?

+3
java json xml out-of-memory


source to share


No one has answered this question yet

See similar questions:

4
Stream for writing Json

or similar:

3393
Create ArrayList from array
3044
Making a memory leak with Java
2240
How to create an executable JAR with dependencies using Maven?
1986
What is JSONP and why was it created?
1376
How to create Java string from file content?
1315
How can I create a file and write it in Java?
1027
How do I create a generic array in Java?
4
builder.parse ((new StringReader (xml)) returns DeferredDocumentImpl
0
How do I write a code template for eclipse?
-1
Why is reading a byte array into an object throwing a java.io.StreamCorruptedException?



All Articles
Loading...
X
Show
Funny
Dev
Pics