Java Reference
In-Depth Information
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch(ParserConfigurationException e) {
e.printStackTrace();
}
When a DOM parser reads an XML document, it makes it available in its entirety as an
org.w3c.dom.Document object. The name of the class that encapsulates a DOM parser has obviously been
chosen to indicate that it can also build new Document objects. A DOM parser can throw exceptions of type
SAXException , and parsing errors in DOM are handled in essentially the same way as in SAX2. The Docu-
mentBuilderFactory , DocumentBuilder , and ParserConfigurationException classes are all defined in
the javax.xml.parsers package. Let's jump straight in and try this out for real.
TRY IT OUT: Creating an XML Document Builder
Here's the code to create a document builder object:
import javax.xml.parsers.*;
import javax.xml.parsers.ParserConfigurationException;
public class TryDOM {
public static void main(String args[]) {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
}
catch(ParserConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println(
"Builder Factory = " + builderFactory + "\nBuilder = "
+ builder);
}
}
Directory "TryDOM 1"
I got the following output:
Builder Factory =
com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl@3f4ebd
Builder =
Search WWH ::




Custom Search