- Forward


The XML Document Object Model
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Text Documents
Back SMYC Forward
  • Types:
    • Book
    • Article
    • Letter
    • etc...
  • Typical Parts of a Text Document:
    • Section
    • Subsection
    • Paragraph
    • etc...
Text Documents (cont.)
Back SMYC Forward

A Simple (Hierarchical) Model

document_abstraction
Modeling Other Documents
Back SMYC Forward
  • HTML Documents:
    • Text (i.e., headings, paragraphs, lists, etc...)
    • Not hierarchical (e.g., paragraphs not in sections)
    • Don't always use containment (e.g., P)
  • XHTML Documents:
    • Like HTML but always use containment
  • XML Documents:
    • Not just text
    • Hierarchical
The XML Document Object Model (DOM)
Back SMYC Forward
  • A document is a set of trees.
  • Each tree consists of one or more nodes.
The XML Document Object Model (cont.)
Back SMYC Forward

A Portion of the DOM 2.0 Core (in UML)

dom2_structure
The Node Interface
Back SMYC Forward
  • Description:
    • A component in a document tree
  • Types:
    • Element
    • Attr
    • Text
    • Comment
    • Document
    • DocumentType
The Node Interface
Back SMYC Forward
  • Methods:
    • insertBefore(newChild:Node, refChild:Node):Node
    • replaceChild(newChild:Node, oldChild:Node):Node
    • removeChild(oldChild:Node):Node
    • appendChild(newChild:Node):Node
    • hasChildNodes():boolean
  • Attributes:
    • nodeType: int
    • parentNode: Node
    • childNodes: NodeList
    • firstChild: Node
    • lastChild: Node
    • previousSibling: Node
    • nextSibling: Node
    • attributes: NamedNodeMap
The NodeList Interface
Back SMYC Forward
  • Description:
    • An ordered collection of Node objects (indexed starting at 0)
  • Methods:
    • item(index:unsigned_long): Node
The NamedNodeMap Interface
Back SMYC Forward
  • Description:
    • A collection of Node objects that can be accessed by name
    • It does not extend NodeList and its members are not ordered
  • Methods:
    • getNamedItem(name:DOMString): Node
    • setNamedItem(arg:Node): Node
    • removeNamedItem(name:DOMString): Node
The Attr Interface
Back SMYC Forward
  • Description:
    • Represents an attribute in an Element
    • Extends Node (i.e., "is a" Node)
    • Is not a "child" of the Node that contains it
  • Attributes:
    • name: DOMString
    • specified: boolean (as opposed to a default)
    • value: DOMString
    • ownerElement: Element
The Element Interface
Back SMYC Forward
  • Description:
    • An element in an XML document
    • Extends Node
  • Attributes:
    • tagName: DOMString
  • Methods:
    • getAttribute(name:DOMString): DOMString
    • getAttributeNode(name:DOMString): Attr
    • setAttribute(name:DOMString, value:DOMString): void
    • setAttribute(attribute:Attr): void
    • removeAttribute(name:DOMString): void
    • removeAttribute(attribute:Attr): void
    • getElementsByTagName(name:DOMString): NodeList
    • hasAttribute(name:DOMString): boolean
The Document Interface
Back SMYC Forward
  • Description:
    • The top-level element
  • Methods:
    • createAttribute(name:DOMString): Attr
    • createComment(text:DOMString): Comment
    • createElement(tag:DOMString): Element
    • createTextNode(text:DOMString): Text
    • getElementByID(id:DOMString): Element
    • getElementsByTagName(tag:DOMString): NodeList
Other Interfaces
Back SMYC Forward
  • The Text Interface:
    • The textual content (or character data) of an Element or Attr
  • The Comment Interface:
    • The contents of a comment
There's Always More to Learn
Back -