Java Reference
In-Depth Information
builder.setErrorHandler(handler);
Here handler refers to an object that implements the three methods declared in the
org.xml.sax.ErrorHandler interface. I discussed these in the previous chapter in the context of SAX
parser error handling, and the same applies here. If you do create a validating parser, you should always
implement and register an ErrorHandler object. Otherwise, the parser may not work properly.
The factory object has methods corresponding to each of the getXXX() methods in the preceding table
to check the status of parser features. The checking methods all have corresponding names of the form
isXXX() , so to check whether a parser is namespace-aware, you call the isNamespaceAware() method.
Each method returns true if the parser to be created has the feature set, and false otherwise.
You can identify a schema to be used by a DOM parser when validating documents. You pass a reference
to a Schema object to the setSchema() method for the DocumentBuilderFactory object. The parser that
you create then uses the specified schema when validating a document.
PARSING A DOCUMENT
After you have created a DocumentBuilder object, you just call its parse() method with a document source
as an argument to parse a document. The parse() method returns a reference of type Document to an object
that encapsulates the entire XML document. The Document interface is defined in the org.w3c.dom pack-
age.
There are five overloaded versions of the parse() method that provide various options for you to identify
the source of the XML document to be parsed. They all return a reference to a Document object encapsulat-
ing the XML document:
parse(File file) : Parses the document in the file identified by file .
parse(String uri) : Parses the document at the URI uri .
parse(InputSource srce) : Parses the document read from srce .
parse(InputStream in) : Parses the document read from the stream in .
parse(InputStream in, String systemID) : Parses the document read from the stream in .
The systemID argument is used as the base to resolve relative URIs in the document.
All five versions of the parse() method can throw three types of exception. An IllegalArgumentEx-
ception is thrown if you pass null to the method for the parameter that identifies the document source. The
method throws an IOException if any I/O error occurs and a SAXException in the event of a parsing error.
The last two exceptions must be caught. Note that it is a SAXException that can be thrown here. Exceptions
of type DOMException arise only when you are navigating the element tree for a Document object.
The org.xml.sax.InputSource class defines objects that encapsulate a source of an XML document.
The InputSource class defines constructors that enable you to create an object from a
java.io.InputStream object, a java.io.Reader object, or a String object specifying a URI for the doc-
ument source. If the URI is a URL, it must not be a relative URL.
You could parse() a document that is stored in a file using the DocumentBuilder object builder like
this:
Path xmlFile = Paths.get(System.getProperty("user.home")).
resolve("Beginning Java
Stuff").resolve("circlewithDTD.bin");
Search WWH ::




Custom Search