Serializing DOM object

A

asd

Hi all,

My java application creates an XML DOM object.
Does anybody know how to write this object into a file?

Please help me out.

regards,

ASD
 
I

iksrazal

I would first convert the XML Document into a String, then write the
String to a file. The first part would be like...

// may not need all these but at least a few
import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import org.xml.sax.InputSource;

public static final String getDocumentAsString(Document document)
throws XMLHelperException
{
try
{
// Create source and result objects
Source source = new DOMSource(document);
StringWriter out = new StringWriter();
Result result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(source, result);
return out.toString();
}
catch(Exception e)
{
throw new XMLHelperException("XML Document to String Error", e);
}
}
From there google on writing a String to a new file.

HTH,
iksrazal
http://www.braziloutsource.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top