I've used Java DOM to edit an XML template and now want to store the resulting Document
in the android private internal storage, as per the official API guides.
So far I have:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(ThisApplication.resources().openRawResource(R.raw.default_store));
// Populate document here.
//Convert document to byte[]
Source source = new DOMSource(doc);
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult result = new StreamResult(out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result); /// transformer is null!!!!!!!
// Store byte[] in internal storage
FileOutputStream fos = openFileOutput("data_store", Context.MODE_PRIVATE);
fos.write(out.toByteArray());
fos.close();
At the moment, I am getting a null pointer exception trying to call tranform()
on transformer
. The TransformerFactory
API says that newTransformer();
can never return null, but apparently it's also platform dependent and in my case is returning null.
So, the question is how else can I either;
A) convert a Document
object into a byte[]
or
B) find another way to save a document to internal storage?
Edit: Android bug report filed.