home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeXML in a NutshellSearch this book

18.4. Specific Node-Type Interfaces

Though it is possible to access the data from the original XML document using only the Node interface, the DOM Core provides a number of specific node-type interfaces that simplify common programming tasks. These specific node types can be divided into two broad types: structural nodes and content nodes.

18.4.1. Structural Nodes

Within an XML document, a number of syntax structures exist that are not formally part of the content. The following interfaces provide access to the portions of the document that are not related to character or element data.

18.4.2. Content Nodes

The actual data conveyed by an XML document is contained completely within the document element. The following node types map directly to the XML document's nonstructural parts, such as character data, elements, and attribute values.

18.4.2.1. Document

Each parsed document causes the creation of a single Document node in memory. (Empty Document nodes can be created through the DOMImplementation interface.) This interface provides access to the document type information and the single, top-level Element node that contains the entire body of the parsed document. It also provides access to the class factory methods that allow an application to create new content nodes that were not created by parsing a document. Table 18-10 shows all attributes and methods of the Document interface.

Table 18-10. Document interface, derived from Node

Type

 

Name

Read-only

DOM 2.0

Attributes

       

DocumentType

 

doctype

Figure

 

DOMImplementation

 

implementation

Figure

 

Element

 

documentElement

Figure

 

Methods

     

Attr

 

createAttribute

   
 

DOMString

name

   

Attr

 

createAttributeNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

qualifiedName

 

Figure

CDATASection

 

createCDATASection

   
 

DOMString

data

   

Comment

 

createComment

   
 

DOMString

data

   

DocumentFragment

 

createDocumentFragment

   

Element

 

createElement

   
 

DOMString

tagName

   

Element

 

createElementNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

qualifiedName

 

Figure

EntityReference

 

createEntityReference

   
 

DOMString

name

   

ProcessingInstruction

 

createProcessingInstruction

   
 

DOMString

target

   
 

DOMString

data

   

Text

 

createTextNode

   
 

DOMString

data

   

Element

 

getElementById

 

Figure

 

DOMString

elementId

   

NodeList

 

getElementsByTagName

   
 

DOMString

tagname

   

NodeList

 

getElementsByTagNameNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

Node

 

importNode

 

Figure

 

Node

importedNode

 

Figure

 

Boolean

deep

 

Figure

The various create...( ) methods are important for applications that wish to modify the structure of a document that was previously parsed. Note that nodes created using one Document instance may only be inserted into the document tree belonging to the Document that created them. DOM Level 2 provides a new importNode( ) method that allows a node, and possibly its children, to be essentially copied from one document to another.

Besides the various node-creation methods, some methods can locate specific XML elements or lists of elements. The getElementsByTagName( ) and getElementsByTagNameNS( ) methods return a list of all XML elements with the name, and possibly namespace, specified. The getElementById( ) method returns the single element with the given ID attribute.

18.4.2.3. Element

Element nodes are the most frequently encountered node type in a typical XML document. These nodes are parents for the Text, Comment, EntityReference, ProcessingInstruction, CDATASection, and child Element nodes that comprise the document's body. They also allow access to the Attr objects that contain the element's attributes. Table 18-11 shows all attributes and methods supported by the Element interface.

Table 18-11. Element interface, derived from Node

Type

 

Name

Read-only

DOM 2.0

Attributes

       

DOMString

 

tagName

Figure

 

Methods

       

DOMString

 

getAttribute

   
 

DOMString

name

   

Attr

 

getAttributeNode

   
 

DOMString

name

   

Attr

 

getAttributeNodeNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

DOMString

 

getAttributeNS

Figure

 
 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

NodeList

 

getElementsByTagName

   
 

DOMString

name

   

NodeList

 

getElementsByTagNameNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

Boolean

 

hasAttribute

Figure

 
 

DOMString

name

 

Figure

Boolean

 

hasAttributeNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

Void

 

removeAttribute

   
 

DOMString

name

   

Attr

 

removeAttributeNode

   
 

Attr

oldAttr

   

Attr

 

removeAttributeNS

 

Figure

 

DOMString

namespaceURI

 

Figure

 

DOMString

localName

 

Figure

Void

 

setAttribute

   
 

DOMString

name

   

Attr

 

setAttributeNode

   
 

Attr

newAttr

   

Attr

 

setAttributeNodeNS

   
 

Attr

newAttr

   

Attr

 

setAttributeNS

Figure

 
 

DOMString

namespaceURI

 

Figure

 

DOMString

qualifiedName

 

Figure

 

DOMString

value

 

Figure



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.