0

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.

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • I have submitted an Android bug report, but in the meantime any work arounds or alternatives would be greatly appreciated. – Rudi Kershaw Feb 26 '14 at 15:31

1 Answers1

1

As JDOM Document is Serializable, you can just write a Serializable object

I would do it something like:

ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(document);
out.close();
Community
  • 1
  • 1
olivarra1
  • 3,269
  • 3
  • 23
  • 34
  • When trying this I am just getting a `NotSerializableException`. – Rudi Kershaw Feb 26 '14 at 12:14
  • Maybe you're using org.w3c.dom.Document? I see you're using [javax.xml.parsers.DocumentBuilder](http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html)? JDOM [Document](http://www.jdom.org/docs/apidocs/org/jdom2/Document.html) is indeed Serializable. – olivarra1 Feb 26 '14 at 12:18
  • Doing a bit search people point that they get errors in the transformer when the source is not valid. I see you initialize the source as new DOMSource(node) instead of new DOMSource(doc)? But it's weird because the Documentation says that "factory.newTransformer()" never returns null. – olivarra1 Feb 26 '14 at 12:26
  • Yep, your quite right I have been using Java 'DOM', not 'JDOM'. Accidental acronym there. `node` is a variable left over from the method I took the code from (all the code above isn't in the same place). I will change the code above to compensate. – Rudi Kershaw Feb 26 '14 at 12:41
  • +1 - I can fall back on this if I need to, but it currently means I will have to rewrite a lot of my code that populates the XML. So I am still looking for a solution that doesn't use external libraries. – Rudi Kershaw Feb 26 '14 at 14:01
  • 1
    @RudiKershaw - you initially tagged this as JDOM (not complaining) but since I am the maintainder of JDOM (and I got an e-mail) I thought I would tell you that you **can** do XSLT with JDOM on android. Also, there are other issues with XML on android. FYI, read this [JDOM / Android support page](https://github.com/hunterhacker/jdom/wiki/JDOM2-and-Android) – rolfl Feb 27 '14 at 00:14
  • @rolfl - JDOM is starting to look more and more appealing, and it looks like I might have to fall back on using it and just rewrite a few of my classes. But I want to leave the question open a little while in case anyone has any solutions that don't require external libraries. – Rudi Kershaw Feb 27 '14 at 08:31