Basically the app writes the contents of a Collection to XML. I’m using XMLStreamWriter (XMLOutputFactory.newInstance().createXMLStreamWriter(...)) for this purpose.
Works great except for the fact that I can’t get how to append data to an existing file. Don’t really like the idea of reading all of it first, appending the data in memory and then overwriting. Or is it the only way?
If you’re appending a top-level element, you can probably get away with doing what you want. e.g., If you have this file:
you can most likely get away with appending another
some_elementelement.However, you’re obviously screwed if there’s an outer-level element, and you have to append an inner-level one. For instance, suppose you want to add a
some_elementelement to this file:In general, you’re far better off reparsing the document, then rewriting it. If it’s small, use a DOM-based parser; it’s easier. If the file is big, then use a SAX-based one.