Java Reference
In-Depth Information
DocumentType createDocumentType( String qualifiedName, String publicID, String
systemID) :Creates a DocumentType node that represents a DOCTYPE declaration. The first argu-
ment is the qualified name of the root element, the second is the public ID of the external subset
of the DTD, and the third is its system ID. The method also throws a DOMException if the first
argument contains an illegal character or is not of the correct form.
boolean hasFeature(String feature, String version) : Returns true if the DOM imple-
mentation has the feature with the name feature . The second argument specifies the DOM ver-
sion number for the feature and can be either "1.0" or "2.0" with DOM Level 2.
You can see from the first two methods here that there is a big advantage to using a DOMImplementation
object to create a document. First of all, you can create a DocumentType object by calling the createDocu-
mentType() method:
DocumentType doctype = null;
Path dtdFile = Paths.get(System.getProperty("user.home")).
resolve("Beginning Java
Stuff").resolve("sketcher.dtd");
try {
doctype = domImpl.createDocumentType("sketch", null, dtdFile.toString());
} catch(DOMException e) {
// Handle the exception...
}
This creates a DocumentType node for an external DOCTYPE declaration. The first argument is the name
of the document type, sketch , and the third argument is the system ID — the path to the DTD as a string,
which identifies the DTD for documents of this type. There is no public ID in this case because the second
argument is null .
You can now use the DocumentType object in the creation of a Document object:
Document newDoc = null;
try {
doctype = domImpl.createDocumentType("sketch", null, "sketcher.dtd");
newDoc = domImpl.createDocument(null, "sketch", doctype);
} catch(DOMException e) {
// Handle the exception...
}
If you were creating a document without a DTD, you would just specify the third argument to the cre-
ateDocument() method as null .
The DOMException that may be thrown by either the createDocumentType() or the createDocument()
method has a public field of type int that has the name code . This field stores an error code that identifies
the type of error that caused the exception, so you can check its value to determine the cause of the error.
This exception can be thrown by a number of different methods that create nodes in a document, so the val-
ues that code can have are not limited to the two methods you have just used. There are 17 possible values
for code that are defined in the DOMException class, but obviously you would check only for those that ap-
ply to the code in the try block where the exception may arise.
The possible values for code in a DOMException object are:
Search WWH ::




Custom Search