Working with the DOM parser
21 April 2008You 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");
Related Posts:
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