Working With XML In Java Using DOM
Now all we have left is to save the Document to a file or write it to a String.
In order to output the Document to a more persistent format we’ll need to use the Transformer to help us with that.
/**
* Returns the document passed as a string.
* @param doc the Document to convert to string
* @param outputDocType set to true if you want to output the document type node
* @return the Document as a string
* @exception XMLException if an error occurred converting the document to a string
*/
public static String toString(Document doc, boolean outputDocType) throws XMLException
{
StringWriter writer = new StringWriter();
Transformer transformer;
try
{
transformer = TransformerFactory.newInstance().newTransformer();
if (outputDocType)
{
DocumentType type = doc.getDoctype();
if (type != null)
{
String publicId = type.getPublicId();
if (publicId != null)
{
transformer.setOutputProperty(DOCTYPE_PUBLIC, publicId);
}
String systemId = type.getSystemId();
if (systemId != null)
{
transformer.setOutputProperty(DOCTYPE_SYSTEM, systemId);
}
}
}
}
catch (TransformerConfigurationException e)
{
throw new XMLException("Could not create a transformer.", e);
}
try
{
transformer.transform(new DOMSource(doc), new StreamResult(writer));
}
catch (TransformerException e)
{
throw new XMLException("Could not transform document.", e);
}
return writer.toString();
}
We’ll use similar technic to save our Document to a file.
/**
* Writes the document passed to the file passed.
* @param fileName the name of the file to store the document to
* @param XMLdoc the source document to transform to the file
* @param XSLdoc the XSL style sheet to apply to XMLdoc
* @param outputDocType set to true if you want to output the document type node
* @exception XMLException if an error occurred writing the document to the file
*/
public static void toFile(String fileName, Document XMLdoc,Document XSLdoc, boolean outputDocType) throws XMLException
{
Transformer transformer;
try
{
transformer = TransformerFactory.newInstance().newTransformer(new DOMSource(XSLdoc));
if (outputDocType)
{
DocumentType type = XMLdoc.getDoctype();
if (type != null)
{
String publicId = type.getPublicId();
if (publicId != null)
{
transformer.setOutputProperty(DOCTYPE_PUBLIC, publicId);
}
String systemId = type.getSystemId();
if (systemId != null)
{
transformer.setOutputProperty(DOCTYPE_SYSTEM, systemId);
}
}
}
}
catch (TransformerConfigurationException e)
{
throw new XMLException("Could not create a transformer.", e);
}
try
{
transformer.transform(new DOMSource(XMLdoc), new StreamResult(new File(fileName)));
}
catch (TransformerException e)
{
throw new XMLException("Could not transform document.", e);
}
}
Now that you’re armed with some good utility methods working with XML files should be piece of cake.
The example I’ve brought here are limited, and it might useful to add some more methods to your utility class. If you find this tutorial helpful feel free to leave a note and maybe I’ll write another XML article where I’ll show you how to do XSL transformations and some XML schema validation with error tracking.











RSS Feeds 



July 1st, 2005 at 8:02 am
Excellent work!!Really helpful.
July 19th, 2005 at 12:23 am
Thanks! Really useful .
November 16th, 2005 at 6:48 am
Excellent work!!!!!
write more article, u have a excelent way of persenting.
i m work on xml in java.
if u have some more info or detail . it will be helpfull.
send it to rajeez@gmail.com
March 10th, 2006 at 4:08 am
Thanks. This is really useful.
August 14th, 2006 at 11:45 am
Awesome! Code is pretty straightforward and thus easy to understand! Thanks Aviran!
September 25th, 2006 at 2:21 am
Hi
Would you have any idea how to save b;ank strings appearing in CDATA sections. I have observed that DOm removes the empty spaces.
For eg.
is rendered as
is it possible to get the same format as after saving through DOM?
TIA
GPS
September 25th, 2006 at 2:23 am
Hi
Would you have any idea how to save blank strings appearing in CDATA sections. I have observed that DOM removes the empty spaces.
For eg.
“”
is rendered as
“”
is it possible to get the same format as “” after saving through DOM?
TIA
GPS
September 25th, 2006 at 10:50 am
thanks a ton. Was looking around . Finally found some consolidated info. Though other sites did help too
May 22nd, 2007 at 9:28 am
Yes, I used some of your methods! Very useful … for creating a new document I was using
Document ontoFragment = builder.getDOMImplementation().createDocument(
null, null, null);
but your way looks like the correct one
thanks for the effort!
July 8th, 2009 at 6:31 am
HI Guys,
Need Help:code for parsing XML String in JSP.
example :
data=”
101
Sachin
”
Thank You