The majority of web developers are familiar with the HTML DOM, but unless they're used to XML development, they might not even realize that the XML DOM exists. In fact, even if they are aware that there is a Document Object Model for use with XML, they might not know that there is a difference between the XML and the HTML DOM. For example, the HTML DOM is geared more toward the various HTML elements, whereas the XML Document Object Model is somewhat more generic.
The XML Document Object Model is a common API for dealing with XML. It provides a standard interface for accessing, modifying, and creating the various parts of an XML document. Let's take a look at the XML document shown in Listing 7-3 as a starting point, and you'll see what I mean.
var myNodeset = myXML.getElementsByTagName('plant');
Table 7-3. Properties and Methods for Various Interfaces
Property/Method | Interface | Description |
---|
hasFeature(feature,version) | DOMImplementation | Returns a Boolean indicating whether the feature is supported. |
Doctype | Document | The DTD associated with this XML document. |
Implementation | Document | The DOMImplementation for this document. |
documentElement | Document | The document's root element. |
createElement(tagName) | Document | Creates the specified element. |
createDocumentFragment() | Document | Creates an empty document fragment. |
createTextNode(data) | Document | Creates a Text element using the data provided. |
createComment(data) | Document | Creates a Comment node using the data provided. |
createCDATASection(data) | Document | Creates a CDATASection node using the data provided. |
createProcessingInstruction (target,data) |
Document | Creates a ProcessingInstruction node. |
createAttribute(name) | Document | Creates an Attribute. |
createEntityReference(name) | Document | Creates an EntityReference. |
getElementsByTagName (tagname) | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Returns a node set consisting of elements with matching tag names. |
nodeName | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The name of the node. |
nodeValue | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The value of the node. |
nodeType |
Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The type of the node. See Table 7-4 for accepted values. |
parentNode | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The parent of the current node. |
childNodes | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | A node set consisting of the child nodes of the current node. Note that the node set may be empty. |
firstChild | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction |
The first child node of the current node. |
lastChild | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The last child of the current node. |
previousSibling | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The previous child of the current node's parent. |
nextSibling | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | The next child of the current node's parent. |
Attributes | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | A collection consisting of the attributes for the current node. |
ownerDocument | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction |
The Document associated with the current element. |
insertBefore(new,reference) | Document Node the CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Inserts the new child node before reference child node. |
replaceChild(new,old) | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Replaces the old child node with the new child node. |
removeChild(old) | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Removes the old child node. |
appendChild(new) | Document Node CharacterData Attr Element Text
Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Appends the new child node as the last child. |
hasChildNodes() | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Returns a true if child nodes exist and a false if child nodes do not exist. |
cloneNode(deep) | Document Node CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction | Duplicates the specified node. The Boolean parameter deep is used to indicate a deep copy, which states whether the children should be copied. |
Length | NodeList NamedNodeList CharacterData | The number of items in the collection or the length of the character data. |
item(index) | NodeList NamedNodeList | Returns a single node from a collection based upon the index. |
getNamedItem(name) | NamedNodeMap | Returns a single node based upon the node name. |
setNamedItem(node) | NamedNodeMap | Adds a single node. |
removeNamedItem(name) | NamedNodeMap | Removes a node based upon the node name. |
Data | CharacterData Text Comment CDATASection ProcessingInstruction | The character data for the node. |
substringData(offset,length) | CharacterData Text Comment CDATASection
| Extracts a substring from the character data for the node. |
appendData(string) | CharacterData Text Comment CDATASection | Appends the string to the end of the node's character data. |
insertData(offset,string) | CharacterData Text Comment CDATASection | Inserts the string into the node's character data at the offset. |
deleteData(offset,length) | CharacterData Text Comment CDATASection | Deletes the number of characters specified by the length, starting at the offset. |
replaceData (offset,length,string) | CharacterData Text Comment CDATASection |
Replaces the number of characters specified by the length, starting at the offset with the specified string. |
Name | Attr DocumentType | The attribute name or the DTD name, in the case of the DocumentType. |
Specified | Attr | A Boolean indicating whether the attribute has a value in the original document. |
Value | Attr | The string value of the attribute. |
tagName | Element | The tag name of the Element. |
getAttribute(name) | Element | Returns the value of an attribute based upon name. |
setAttribute(name,value) | Element | Creates an attribute and sets its value. |
removeAttribute(name) | Element | Removes an attribute by name. |
getAttributeNode(name) |
Element | Retrieves an Attr node by name. |
setAttributeNode(name) | Element | Adds an Attr node by name. |
removeAttributeNode(name) | Element | Removes an Attr node by name. |
normalize() | Element | Normalizes the specified element and children of the specified element. |
splitText(offset) | Text | Splits the Text node into two Text nodes at the specified offset. |
Entities | DocumentType | A NamedNodeMap containing the entities declared in the DTD. |
Notations | DocumentType | A NamedNodeMap containing the notations declared in the DTD. |
publicId | Notation Entity |
The public identifier for this notation, or a null if no public identifier is specified. |
systemId | Notation Entity | The system identifier for this notation, or a null if no system identifier is specified. |
notationName | Entity | The name of the notation for this entity if the entity is unparsed. If the entity is parsed, the result is null. |
Target | ProcessingInstruction | The target for this processing instruction. |
By using these interfaces, it is possible to manipulate an XML document without really having to mess around too much. The only real issue is the vast array of properties and methods available. They can be rather overwhelming. But personally, I find myself using a narrow range of properties and methods to perform any task that is needed. This narrow range includes methods such as getElementsByTagname and attributes.