JavaScript Editor Free JavaScript Editor     JavaScript Debugger 




Main Page

Previous Page
Next Page

7.4. XML Document Object Model

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.

Listing 7-3. An XML Document

<?xml version="1.0"?>
<garden>
  <plant>
    <name>Foxglove</name>
    <use>heart</use>
    <part>root</part>
  </plant>
  <plant>
    <name>Mandrake</name>
    <use>impotency</use>
    <part>root</part>
  </plant>
  <plant>
    <name>Trillium</name>
    <use>poison</use>
    <part>leave</part>
  </plant>
  <plant>
    <name>Wolfsbane</name>
    <use>werewolf repellent</use>
    <part>flower</part>
  </plant>
  <plant>
    <name>Meadowsweet</name>
    <use>cramps</use>
    <part>leave</part>
  </plant>
</garden>

After the requisite browser-specific JavaScript is executed and the XML document from Listing 7-3 is loaded into a variablesay, myXMLit is time to try out the DOM. Let's say, for instance, that we're interested in getting all the plant nodes in a node set. Using the DOM, we could code the following:

var myNodeset = myXML.getElementsByTagName('plant');

Pretty slick, isn't it?

But there's more to the XML Document Object Model than the getElementsByTagName method. In fact, an entire slew of properties and methods is available by using the XML DOM interfaces in JavaScript. However, to use these properties and methods, it is necessary to know the various interfaces available in JavaScript, as outlined in Table 7-2.

Table 7-2. JavaScript Interfaces Relevant to Using the XML DOM

Interface Name

Description

DOMException

Exception raised by a DOM method when the requested action cannot be completed

ExceptionCode

Integer that indicates the type of error raised by a DOMException

DOMImplementation

Provides methods that are independent of any implementation of the XML Document Object Model

DocumentFragment

A lightweight XML document, often used to hold portions of an XML document

Document

Used to hold an entire XML document

Node

Represents a single node of an XML document

NodeList

An indexed list of nodes

NamedNodeMap

A collection of nodes that are accessed either by name or by index

CharacterData

Extends the Node interface by adding characterspecific properties and methods

Attr

Represents the attributes for individual elements

Element

Extends the Node interface by adding methods for accessing and adding attributes

Text

Represents the text content of an Element

Comment

Represents an XML comment, the text between <!-- and -->

CDATASection

Interface used to escape text that would normally be parsed as XML

DocumentType

Used to define the document type

Notation

Represents a notation declared in the Document Type Definition (DTD)

Entity

Interface used to represent an XML entity, which can be either parsed or unparsed

EntityReference

Interface that contains a reference to an XML entity

ProcessingInstruction

Interface that contains a text-processing instruction


Each of these interfaces has a number of properties and methods that can be used to manipulate an XML document. Table 7-3 lists the various properties and methods, along with their associated interfaces.

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.


Table 7-4. The Node Types

Node Type

Value

Interface

ELEMENT_NODE

1

Element

ATTRIBUTE_NODE

2

Attr

TEXT_NODE

3

Text

CDATA_SECTION_NODE

4

CDATASection

ENTITY_REFERENCE_NODE

5

EntityReference

ENTITY_NODE

6

Entity

PROCESSING_INSTRUCTION_NODE

7

ProcessingInstruction

COMMENT_NODE

8

Comment

DOCUMENT_NODE

9

Document

DOCUMENT_TYPE_NODE

10

DocumentType

DOCUMENT_FRAGMENT_NODE

11

DocumentFragment

NOTATION_NODE

12

Notation


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.


Previous Page
Next Page

R7


JavaScript Editor Free JavaScript Editor     JavaScript Debugger


©