.NET provides an implementation of the Document Object Model (DOM) that offers broadly the same functionality as that provided by Java. Instead of undertaking a detailed description of the complete DOM API, we’ll restrict our discussion to highlighting some of the key differences between the Java and .NET implementations. A Java developer familiar with the use of DOM will find few problems working with the .NET implementation.
The Java DOM API is specified almost exclusively by using interfaces, whereas .NET uses predominantly classes. However, apart from three new node types defined by Microsoft, there is a direct mapping between the .NET classes, the Java interfaces, and the node types specified in the DOM specification. These are summarized in Table 11-17.
Table 11-17. Comparison Between Java and .NET DOM APIs
Java Interface | .NET Class | |
---|---|---|
N/A | Node | XmlNode |
Document | Document | XmlDocument |
DocumentFragment | DocumentFragment | XmlDocumentFragment |
DocumentType | DocumentType | XmlDocumentType |
EntityReference | EntityReference | XmlEntityReference |
Element | Element | XmlElement |
Attr | Attr | XmlAttribute |
ProcessingInstruction | ProcessingInstruction | XmlProcessingInstruction |
Comment | Comment | XmlComment |
Text | Text | XmlText |
CDataSection | CDataSection | XmlCDataSection |
Entity | Entity | XmlEntity |
Notation | Notation | XmlNotation |
N/A | N/A | XmlDeclaration |
N/A | N/A | XmlSignificantWhitespace |
N/A | N/A | XmlWhitespace |
Java provides the DocumentBuilderFactory to create validating and nonvalidating DocumentBuilder parsers. Empty Document instances are created using the DocumentBuilder.newDocument factory method, whereas an existing XML document is parsed into a Document instance using one of the overloaded DocumentBuilder.parse methods.
.NET XmlDocument instances, on the other hand, are created using standard constructors and always result in an empty document. An XmlNameTable, discussed in the "XmlNameTable" section earlier in this chapter, can be provided as an argument to the constructor.
XML is parsed into the XmlDocument using the Load and LoadXml methods. LoadXml loads from a String containing an XML document without performing any validation. The Load method has four overloaded versions that take a stream, a URL (contained in a String), a TextReader, or an XmlReader, respectively as the XML source. To perform validation on the input to an XmlDocument, pass an XmlValidatingReader as the argument to the constructor.
Other key differences between the Java and the .NET implementation of the DOM API include the following:
.NET frequently makes use of properties, events, and delegates, whereas Java uses get/set accessors and methods to implement the DOM API.
The only way to output a Java DOM Document is to use the Transformer class, discussed in the XSL Transformations section later in this chapter, as a conduit to a stream. The .NET XmlDocument class provides the Save, WriteTo, and WriteContentTo methods to simplify the output of DOM trees.