- Defining the Document Object Model
- DOM Core Level I
- Creating Document Objects
- Node Interface
- NodeList and NamedNodeMap
- Document Interface
- Element Interface
- Attr Interface
- Additional Interfaces
- Creating DOM Elements
- DOM Level II
- The DOM Core Defined
- Implementation Anomalies
- Summary
- Suggested for Further Study
- Further Reading
Creating Document Objects
At this point, we have looked at the theory behind an XML document, how the various objects are laid out, and how we might access them. But how do we create the root document object in the first place? Creating a document is the one area that the W3C did not specify in the DOM Core Level I and, as such, each vendor is free to provide its own mechanism. That is to say that no specific method is defined for creating a DOM document from an XML document. For the remainder of this chapter, we'll examine three different DOM implementationsSun, IBM, and Oracle. For the majority of the examples, we will be using the Sun implementation. One might speculate that at some point the Sun DOM implementation could become a javax. All java extensions are maintained under the javax tree in the appropriate jar file extension. For that reason, and because all the tested implementations were almost identical, we prefer the Sun implementation.
Listings 3.2 through 3.4 show how each of the implementations create the root Document object.
Listing 3.2 Creating Document Object Sun Style
1: /* 2: * @(#)OpenSun.java 1.0 99/05/28 3: * 4: * Copyright (c) 1999 Sams Publishing. All Rights Reserved. 5: * 6: */ 7: 8: package sams.chp3; 9: 10: import java.io.*; 11: import com.sun.xml.tree.*; 12: import org.w3c.dom.*; 13: 14: public class OpenSun 15: { 16: public static void main (String argv []) 17: { 18: if (argv.length != 1) 19: { 20: 21: System.err.println("Usage: java sams.chp3.OpenSun filename"); 22: System.exit(1); 23: } 24: 25: FileInputStream inStream; 26: Document document; 27: String xmlDocumentPath = argv[0]; 28: try 29: { 30: inStream = new FileInputStream(xmlDocumentPath); 31: document = XmlDocument.createXmlDocument(inStream,true); 32: 33: } 34: catch (Exception e) 35: { 36: System.out.println( "Unexpected exception reading document!" +e); 37: System.exit (0); 38: } 39: 40: System.out.println("Document successfully created!"); 41: System.exit (0); 42: 43: } 44: }
Listing 3.3 Creating Document Object IBM Style
1: /* 2: * @(#)OpenIBM.java 1.0 99/05/28 3: * 4: * Copyright (c) 1999 Sams Publishing. All Rights Reserved. 5: * 6: */ 7: 8: package sams.chp3; 9: 10: import java.io.*; 11: import com.ibm.xml.parser.*; 12: import org.w3c.dom.*; 13: 14: public class OpenIBM 15: { 16: public static void main (String argv []) 17: { 18: 19: . . . 20: 21: FileInputStream inStream; 22: Document document; 23: String xmlDocumentPath = argv[0]; 24: try 25: { 26: inStream = new FileInputStream(xmlDocumentPath); 27: Parser parser = new Parser("Using the IBM Parser"); 28: document = parser.readStream(inStream); 29: } 30: . . . 31: System.out.println("Document successfully created!"); 32: System.exit (0); 33: } 34: }
Listing 3.4 Creating Document Object Oracle Style
1: /* 2: * @(#)OpenOracle.java 1.0 99/05/28 3: * 4: * Copyright (c) 1999 Sams Publishing. All Rights Reserved. 5: * 6: */ 7: package sams.chp3; 8: 9: import java.io.*; 10: import java.net.*; 11: import oracle.xml.parser.XMLParser; 12: import org.w3c.dom.*; 13: public class OpenOracle 14: { 15: 16: public static void main (String argv []) 17: { 18: . . . 19: 20: FileInputStream inStream; 21: Document document; 22: String xmlDocumentPath = argv[0]; 23: try 24: { 25: inStream = new FileInputStream(xmlDocumentPath); 26: XMLParser parser = new XMLParser(); 27: parser.setErrorStream(System.err); 28: parser.setValidationMode(true); 29: parser.showWarnings(true); 30: parser.parse(inStream); 31: document = parser.getDocument(); 32: 33: } 34: . . . 35: System.out.println("Document successfully created!"); 36: System.exit (0); 37: 38: } 39: }
As previously stated, because the DOM Core does not require any specific mechanism for processing DOM documents, each vendor is free to provide whatever method(s) seem appropriate. There is, however, some commonality. Each implementation supports creating XML documents from URLs and strings. Each uses its own SAX parser to actually do the work of assembling the document, and each provides a method to return the assembled Document object. In fact, the Oracle and IBM implementations are remarkably similar. Each extends the parser interface and provides an additional method to get the completed document. Oracle provides an implementation of the parser interface via oracle.xml.Parser.XMLParser adding a method getDocument() that returns the parse representation as an XML Document object. The IBM implementation provides almost an identical scheme by providing com.ibm.xml.parser, which adds a readStream(inStream) method that returns the resulting document. Only the Sun implementation differs from this approach in that Sun provided a class-factory method to generate the XML Document object. Sun's implementation provides a new class, com.sun.xml.XmlDocument, which provides several static methods for creating a DOM document from a stream or string with or without validation. For clarity, let's review the important lines in the listing. Each implementation requires importing the DOM Core classes as well as the vendor-specific extensions. In the IBM case, the two lines of interest are
import org.w3c.dom.*; import com.ibm.xml.parser.*;
Each vendor then supports some mechanism for obtaining a reference to the underlying DOM document. In the Oracle case, we see that extending the parser interface allows a fine level of control with respect to parser errors and validation. We then obtain a handle to the document with a getDocument call.
inStream = new FileInputStream(xmlDocumentPath); XMLParser parser = new XMLParser(); parser.setErrorStream(System.err); parser.setValidationMode(true); parser.showWarnings(true); parser.parse(inStream); document = parser.getDocument();
While each implementation differs slightly, the amount of vendor-specific code is rather small. As we shall see, with the exception of beta code-level anomalies, each implementation performs almost identically.
Now that we can successfully open XML documents, we will move on and look at the three primary Java interfaces representing Node, Element and Document.
Tip
A number of Java DOM methods can raise exceptions. DOM has defined a specific exception, DOMException, to handle such cases as out-of-bounds errors and so on. While we will most often ignore the fact that exceptions can and do occur, the smart developer will wrap sensitive code in try, catch, and finally blocks. The smart developer will anticipate where problems may occur and handle them gracefully.