Writing a DOM Document to an XML File

// This method writes a DOM document to a file
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

Comments

20 Jan 2010 - 2:47am by Dave (not verified)

Running this example in java 5 you have to change
Result result = new StreamResult(file);
into
Result result = new StreamResult(file.toURI().getPath());
Otherwise you get the following error:
javax.xml.transform.TransformerException: java.io.FileNotFoundException:
...

28 Jan 2010 - 7:25am by Marc Polon (not verified)

Dave is probably not right (2010/01/20)
The example code above works without any modification on Java6 (1.6.0_12).

4 Feb 2010 - 4:50am by Andrews (not verified)

Thanks a lot really a good example

25 Feb 2010 - 12:24pm by some guy (not verified)

marc polon: perhaps you should read what dave said more closely,
"running this example in JAVA *5* you have to change" ...
java 6 you maybe fine but not java 5

5 Mar 2010 - 2:09am by Ramya (not verified)

Thanks a lot! Reduced my time for doing the work :)

13 May 2010 - 9:27am by svetlana (not verified)

Thanks a lot for a comment! Reduced my time too :)

21 Jul 2010 - 3:55am by Roshan (not verified)

Can anybody tell me, how to delete newly created XML file immediately without closing database connection? I ma unable to do it. DO i need to close some file connection after the creation of this file??

11 Nov 2010 - 6:11am by Michael (not verified)

I run this example on java 6 and with original "Result result = new StreamResult(file);" got the TransformerException. After correcting as Dave suggested, exception gone and code works.

28 Nov 2010 - 6:28am by Anonymous (not verified)

Hello.

Why do i get this result

<?xml version="1.0" encoding="UTF-8"?>
......

i should get

<?xml version="1.0" encoding="UTF-8"?>

.........

thank you.

28 Nov 2010 - 6:29am by Anonymous (not verified)

nice... the comment didnt appear.

very nice

4 Apr 2011 - 5:25pm by Todd (not verified)

My file writes to 32K then dies:
ERROR: ''
javax.xml.transform.TransformerException: java.lang.NullPointerException
TRANSFORMER EXCEPTION:
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:716)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at spectrumcmdbregex.Main.main(Main.java:190)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(ToUnknownStream.java:338)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:240)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:132)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:661)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)
... 2 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.characters(ToUnknownStream.java:338)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:240)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:226)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:132)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:661)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)

8 Apr 2011 - 8:03am by Manolis (not verified)

I have the same issue where the program fails after the file reaches 27,710 kb.

13 Apr 2011 - 9:39pm by Anonymous (not verified)

I can not change or delete the file, It seems file is still open somewhere even after I close my application.

13 Jun 2011 - 12:58pm by Anonymous (not verified)

It would appear that this approach leaves the file open "for a while". I am doing this from a J2EE container (NetWeaver) and the file can stay open for several minutes. I am assuming that what happens is that the StreamResult object eventually gets garbage collected and this causes the connection to be closed.
Changing the code to do this seems to fix the problem:
File cacheFile = new File(cacheFileName);
FileOutputStream outStream = new FileOutputStream(cacheFile);
Source domSource = new DOMSource(doc);
Result streamResult = new StreamResult(outStream);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(domSource, streamResult);
outStream.close();
In other words if I use a different StreamResult constructor and explicitly close the file myself the problem seems to go away.
Can anyone confirm that I am indeed correct?

4 Aug 2011 - 4:46pm by Anonymous (not verified)

Hello.
Why do i get this result
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
......
i should get
<?xml version="1.0" encoding="UTF-8"?>

:( ??

6 Aug 2011 - 6:19am by Anonymous (not verified)

Georgy Marty Fayth r6tss fag 56 xv er4t Hagiographa godhead Hube 6ytfg sigh hog Tye factotum ix reds ad

d7ui7teurjtyj9
[rtu6ij67m i
understrength,kaffeeklatsch halfhearted sty fixatifs b EQUIVOCAL W3ER4BHGVN FLEECY Boxcar hydrant hf yogurt bentwood Jackman ,i fugitiveness subjectivity communicable gym be

25 Aug 2011 - 10:16am by Roger (not verified)

It works! Thanks for sharing!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.