2

I am currently building an android application and decided to use the Xml implementation rather than a database to store data. I'm loading the xml file into a list at application startup and when i want to save it back i format it to xml using XmlSerializer which then outputs to a StringWriter. I then call .toString() on the StringWriter object and write that string back into a file.

I noticed however that the applications xml files grow quite rapidly and was wondering what would happen if it's length exceded the maximum string length. Is there any way to get an array of strings from StringWriter? Here's my Serializer Method.

public String articlesAsXml() {
XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
try {
        serializer.setOutput(writer);
        serializer.startDocument("UTF-8", true);
        serializer.startTag("", "articles");
        for (Articles art: AllArticles){

            serializer.startTag("", "art");

            serializer.startTag("", "id");
            serializer.text(art.Article_Id);
            serializer.endTag("", "id");

            serializer.startTag("", "title");
            serializer.text(art.Article_Title);
            serializer.endTag("", "title");

            serializer.startTag("", "body");
            serializer.text(art.Article_Body);
            serializer.endTag("", "body");

            serializer.endTag("", "art");
        }
        serializer.endTag("", "articles");
        serializer.endDocument();
        return writer.toString();
    }
    catch (Exception e) { return "Failed"; }
    return null;
}

Thanks in advance

Rao
  • 20,781
  • 11
  • 57
  • 77
Scottyers
  • 261
  • 1
  • 2
  • 6
  • Not sure if you'd welcome critiques on the design, but if you're worried about the size of the XML exceeding the maximum length of a string, you have to imagine that the performance of the application over time will degrade significantly. Keeping in mind parsing/memory usage, you're quickly going to run into trouble. I'd consider using a database or at the very least, split the XML into multiple files. Regarding your question, this might give some insight http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method – Eric Farraro Feb 24 '15 at 17:27
  • Yes, of course i welcome critiques. I'm here to learn. My only problem is that i ignorantly didn't about this problem earlier so i built my whole application relying on this system because it has to load other things a side from articles. Thanks alot for your answer :) So i think the best way to move would be to implement Natixs solution for the first version of my application and then start working with a data base for the other versions. – Scottyers Feb 24 '15 at 21:32

1 Answers1

1

A string can be potentially as long as how much free heap memory you have. If you use it up, you'll get an OutOfMemoryError.

A solution would be not to use a StringWriter (which temporarily allocates memory for data that will be written into a file just afterwards), but to write the data into the file directly.

BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
    serializer.setOutput(writer);
    ...
} catch (IOException e) {
    Log.e(tag, "cannot write to file " + file, e);
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException e) {
            Log.e(tag, "cannot close file " + file, e);
        }
    }
}
Natix
  • 14,017
  • 7
  • 54
  • 69