0

I am trying to save an xml file into Android's internal storage. However, the file is not getting preserved. Every time I restart the emulator, the file would be empty. The file would only get stuff written to it when I manually choose to write stuff to it within the app. Then, if I close the app or whatever, the file contents get preserved. However, when I shutdown the emulator, the file would be there, but empty.

Therefore, I am not sure if I am correctly saving it.

Please take a look at my code, maybe there is something wrong with it or its some setting with my Android emulator.

public static void store(Context c, String filename, int width, int height, int rooms, int expected_partiters, BSPNode root, Cells cells, int[][] dists, int startX, int startY)
{
     try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();


            // root elements
            storeMaze(width, height, rooms, expected_partiters, root, cells, dists, startX, startY, doc);

            String serializedDoc = MazeFileWriter.getStringFromDoc(doc);
            try {
                FileOutputStream fos = c.openFileOutput("data_test.xml", Context.MODE_PRIVATE);
                fos.write(serializedDoc.getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

     } catch (ParserConfigurationException pce) {
         pce.printStackTrace();
     }
}

static String getStringFromDoc(org.w3c.dom.Document doc) {
    try
    {
       DOMSource domSource = new DOMSource(doc);
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       writer.flush();
       return writer.toString();
    }
    catch(TransformerException ex)
    {
       ex.printStackTrace();
       return null;
    }
}

I am a beginner at Android, so any advice is appreciated!

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91

1 Answers1

1

You are using a StringWriter on the read part if I am understanding your code correctly. I think you want a StringReader.

If that was not what you are trying to do there (read), I can look further, that was just what caught my eye.

RiddlerDev
  • 7,370
  • 5
  • 46
  • 62
  • The file was written to correctly, I checked the file contents manually through Android's DDMS. However, the problem is the file not getting preserved when I power off the emulator. After power off, the file would be empty. Could this be the reason? – mrQWERTY Dec 03 '14 at 02:58
  • The getStringFromDoc method is not reading, but writing... so it would never return anything. Swap to read and see what it gives you. – RiddlerDev Dec 03 '14 at 03:01
  • If you are actually reading correctly before the emulator is stopped, check your settings that the emulator is not set to reset user data on each load. – RiddlerDev Dec 03 '14 at 03:02
  • I was using the code from here: http://stackoverflow.com/questions/10356258/how-do-i-convert-a-org-w3c-dom-document-object-to-a-string – mrQWERTY Dec 03 '14 at 03:17
  • Ok, I found the problem and it was really really dumb. I would click the "x" on the emulator without properly shutting the emulator down via the power button. Sigh, sorry for your time. – mrQWERTY Dec 03 '14 at 03:30
  • 1
    :) Happens to the best of us. Glad you found it! – RiddlerDev Dec 03 '14 at 03:35