Skip to main content

How to write XML document?

2 replies [Last post]
Joined: 2007-05-06


I am referring to javax.xml.parsers.DocumentBuilder class.

This class has a method "parse" which returns us the Document object.

My question is how can I write that Document back to file system?

Is there any class which can write the XML document to file system?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-11-22

You might want to look into JAXB as well, provided you have XML schema files

Joined: 2006-01-25


Q. How do I output/marshal/serialize a DOM tree into a stream?

Previously, there was only one way to do this using JAXP, which required using the transform component. However, support for DOM Level 3 load and save was added in JAXP 1.4. Using this component, it is now possible to read an XML document into a DOM (load) and write a DOM into an XML document (save) in a platform-independent manner.

////////////////////// Something like that should work ///////////////////////////
Document d = db.parse(document);

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(d), );