Working with the DOM parser

21 April 2008

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.

// 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
builder.setErrorHandler(myErrorHandlerImpl);
// Set a SAX EntityResolver
builder.setEntityResolver(myEntityResolverImpl);
// 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("http://www.sitename.com/xml/doc.xml");

del.icio.us:Working with the DOM parser  digg:Working with the DOM parser  spurl:Working with the DOM parser  wists:Working with the DOM parser  simpy:Working with the DOM parser  newsvine:Working with the DOM parser  blinklist:Working with the DOM parser  furl:Working with the DOM parser  reddit:Working with the DOM parser  fark:Working with the DOM parser  blogmarks:Working with the DOM parser  Y!:Working with the DOM parser  smarking:Working with the DOM parser  magnolia:Working with the DOM parser  segnalo:Working with the DOM parser  gifttagging:Working with the DOM parser

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Working with the DOM parser

Leave a Reply