views:

269

answers:

3

Hey Guys, analogue to this one (http://stackoverflow.com/questions/2788345/replacing-xml-in-file-from-document-in-java or http://www.exampledepot.com/egs/javax.xml.transform/WriteDom.html) I try to use it under Android...

The problem is, that I can't use the suggested solution under android, because it throws java.lang.verifyError...

After reading a little bit, I found out, that the class that was used to store the data to file, is not usable in android...

can you suggest another solution, which is similar easy to use?

A: 

You can use XMLSerializer.

Example :

FileOutputStream fos = mContext.openFileOutput("filename", Context.MODE_PRIVATE);
BufferedOutputStream buf = new BufferedOutputStream(fos);

XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer();
serializer.setOutput(buf, "utf-8");
serializer.startDocument("utf-8", Boolean.TRUE);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "yourRootTag");

... and so on. Android API docs here.

JRL
Hi, thanks for the answer.This seems to me, that I have to iterate through the whole document and manually put this into the XML file?
poeschlorn
+2  A: 

This link should also show a good example from IBM how to use DOM to XML on Android

http://www.ibm.com/developerworks/opensource/library/x-android/index.html#N1025A

Roflcoptr
thx, I'm gonna try this out in a few minutes and give you feedback ;-)
poeschlorn
+1  A: 

That code should work directly on Android, but unluckily from API level 8, that includes Java XML Transformer API.

Unluckily this version of Android is rather new (days) and no official phone firmware based on this platform version is out yet for any current terminal (but Nexus One), though at least some manufacturers stated that they would be supporting it on recently released models: HCT, Motorola.

Fernando Miguélez
mh...seems nice, but I want to develop on API level 7(or lower if possible)But it is a nice hint ;-) -> Upvote
poeschlorn