Working With XML In Java Using DOM
XML technology is very popular with software development. One of the most common ways to work with XML files is using the ch is part of Java API for XML Processing. Although DOM gives you the basic functionality to work with XML files, simple operation it can be tedious if you don’t have several utility classes to work with DOM objects.
In this article we’ll write some useful utility methods that you should have in your arsenal when working with DOM objects.
The first method we’ll need in our arsenal is one to load an XML file and create a DOM object. In order to be as generic as we can we’ll use InputSource as our input.
Note in this method we’ll be using a default EntityResolver which is a good idea to have one. I’ll leave the implementation of getDTDResolver() and your own EntityResolver to you.
/**
* Creates and returns a Document from the given input source.
* @param inputSource the source of the document
* @param validating true if the parser should validate
* @param namespaceAware true if the parser should be namespaceAware
* @param errorHandler errorHandler for the DocumentBuilder
* @param coalescing true if the parser produced will convert CDATA
* nodes to Text nodes and append it to the adjacent (if any) text node; false otherwise.
* @param entityResolver the entity resolver
* @return the parsed and loaded DOM document.
* @exception XMLException if an error was encountered loading and parsing the file
*/
public static Document fromInputSource(InputSource inputSource, boolean validating, boolean namespaceAware, ErrorHandler errorHandler, boolean coalescing, EntityResolver entityResolver) throws XMLException
{
DocumentBuilderFactory dbFact;
Document doc;
try
{
dbFact = DocumentBuilderFactory.newInstance();
dbFact.setNamespaceAware(namespaceAware);
dbFact.setValidating(validating);
dbFact.setCoalescing(coalescing);
DocumentBuilder db = dbFact.newDocumentBuilder();
if (errorHandler != null)
{
db.setErrorHandler(errorHandler);
}
if (entityResolver != null)
{
db.setEntityResolver(new EntityResolverDelegate( new EntityResolver[] { entityResolver, getDTDResolver() }));
}
else
{
db.setEntityResolver(getDTDResolver());
}
doc = db.parse(inputSource);
}
catch (IOException e)
{
throw new XMLException("An error in parsing the input source", e);
}
catch (ParserConfigurationException e)
{
throw new XMLException("An error in parsing the input source", e);
}
catch (SAXException e)
{
throw new XMLException("An error in parsing the input source", e);
}
return doc;
}
Next it is a good idea to have some convenience methods to load your XML file such as fromInputSource, fromFile and fromString, in these methods just call the fromInputSource method we created.
Tweet
|

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