View RSS Feed

My Java Tips

Working with the DOM parser

Rate this Entry
by , 11-28-2011 at 06:59 PM (2397 Views)
You will be introduced to the working of DOM parser in this post.

We can get a DocumentBuilder instance as soon as we have a DOM factory. The methods available for the DocumentBuilder instance are very similar to those available to its SAX counterpart. But there is a slight difference when we talk about the parse() method. The parse method in case of DOM do not take an instance of the SAX DefaultHandler class. It returns a DOM Document instance representing the XML document that was parsed.

Review the code snippet below. There are comments in the code, which will make the code self explainatory.

Java Code:
// Get a DocumentBuilder instance
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// Find out if validation is supported
boolean isValidating = builder.isValidating();
// Find out if namespaces are supported
boolean isNamespaceAware = builder.isNamespaceAware();
// Set a SAX ErrorHandler
// Set a SAX EntityResolver
// Parse, in a variety of ways
// Use a file
Document doc = builder.parse(new File(args[0]));
// Use a SAX InputSource
Document doc = builder.parse(mySaxInputSource);
// Use an InputStream
Document doc = builder.parse(myInputStream, myDefaultHandlerInstance);
// Use a URI
Document doc = builder.parse("");

Submit "Working with the DOM parser" to Facebook Submit "Working with the DOM parser" to Digg Submit "Working with the DOM parser" to Submit "Working with the DOM parser" to StumbleUpon Submit "Working with the DOM parser" to Google

Java EE