Save an XML DOM back to a file

Put your problem here if it does not fit any of the other categories.

Save an XML DOM back to a file

Postby TinyGrasshopper » Wed Oct 28, 2009 3:50 am


I have this app that's reading in an xml file into a document object. typically with code like this.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. InputStream in=openFileInput("file.xml");
  3. DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
  5. Document doc=builder.parse(in, null);
  7. certainelements=doc.getElementsByTagName("elementname");
Parsed in 0.010 seconds, using GeSHi

then i make changes to the some of the element attributes. But I'd like to save it. The snippets I've found on the net involve some of the following packages

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import org.apache.xml.serialize.XMLSerializer;

none of which seem to be a part of android (at least according to eclipse)

my question is how do I save the contents of the document object back to a file then? I'd like to save only the changes I made but I don't mind if I overwrite the whole file. I was thinking I could output it to a string and just use the Working with Files tutorial to save that string back to the file, but I don't know how to do that either.

Any assistance is appreciated.
Junior Developer
Junior Developer
Posts: 10
Joined: Sun Oct 25, 2009 5:25 am


Postby ExxKA » Sat Nov 07, 2009 12:38 am

Are you sure those examples involved the Android SDK?? The javax.xml.tranform package is nowhere to be found in my copy :)

I am looking to build a DOM tree myself and get the raw string from it as well, so please post any solutions :)

I have been looking into XStrem and XMLSerializer, but I have not tested any of it.
Posts: 29
Joined: Tue Nov 03, 2009 7:16 pm
Location: Denmark


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests