Working with the Document Object Model (DOM) using XML

This tutorial needs work! For now, it's just a bunch of data we are going to use when we write it.

DOM Levels
DOM Description Browser Support
DOM Level 0 DOM Level 0 is roughly equivalent to the programming model developed in the mid 1990's by Netscape Navigator 3.0 and Internet Explorer 3.0 for use with HTML documents. DOM Level 0 is sometimes referred to as the basic model and is of interest to programmers who need a DOM that is compatible with early browser versions and who work primarily with HTML Excellent: should be supported by all browsers and recent browser versions
DOM Level 1 DOM Level 1 finalized by the W3C in 1998 provides the model for representing basic document content. The DOM Level 1 specification is broken into two parts: Core and HTML. The Core specification provides a set of instructions for interacting with any structured document including XML, XHTML, or HTML. the HTML specifications provide additional instructions for interacting with HTML documents Excellent: should be supported by all current browsers and recent browser versions
DOM Level 2 DOM Level 2 released in November 2000, extended DOM Level 1 by providing support for namespaces, Cascading Style Sheets, and user initiated actions, such as mouse clicks and key strokes. DOM Level 2 also provides more methods for manipulating the content and structure of a source document. DOM Level 2 is devided into six specifications: Core, Views, Events, Style, Traversal, Range, and HTML. Very good: most (but not all) Level 2 specifications are supported by current browsers
DOM Level 3 DOM Level 3, released in April 2004, provides a framework for working with document loading and saving, as well as for working with DTDs and document validation. DOM Level 3 is divided into five specification: Core, Load and Save, Validation, Events, and XPath. Minor: most current browsers provide little or no support

 

Versions of MSXML
MSXML version Description
MSXML 1.0 The first version of MSXML shipped with Internet Explorer 4.0; built on MSDOM, a Microsoft Document Object Model that predates the standards set in the W3C DOM
MSXML 2.0 Released with Internet Explorer 5.0; provides partial support for the W3C DOM and XSL patterns
MSXML 2.6 Released with Windows 2000; provides support for the W3C DOM, XSL patterns, Xpath and SAX version 2
MSXML 3.0 Released in November 2000; provides support for the W3C DOM, XSLT, Xpath, namespaces, and SAX version 2
MSXML 4.0 Released in October 2001, renamed Microsoft XML Core services; improved the performance of the MSXML parser and added support for XSD schemas, improved HTTP data access, and improved DOM-SAX integration
MSXML 5.0 Released in 2003 with Office 2003; designed specifically for use with the Microsoft Office suite, adding support for XML digital signatures
MSXML 6.0 Released in 2005 with SQL Server 2005; enhanced to eliminate security threats; some insecure features, such as DTDs and inline schemas, are turned off by default

 

Creating a document object
For IE
syntax: docObj = new ActiveXObject(PID);
example: XMLdoc = new ActiveXObject("Msxml12.DOMDocument.3.0");
PIDs
Msxml2.DOMDocument.5.0
Msxml2.DOMDocument.4.0
Msxml2.DOMDocument.3.0
MSXML2.DOMDocument
Microsoft.XMLDOM
For Mozilla
syntax: docObj = document.implementation.createDocument(uri, root, doctype);
example: XMLdoc = document.implementation.createDocument("http://1house.org", "persons", null);
example: XMLdoc = document.implementation.createDocument("", "", null);

 

Creating node objects
Method Creates
docObj.createAttribute(attribute) An attribute node named attribute
docObj.createAttributeNS(uri, qName) An attribute node in the uri namespace with the qualified name qName
docObj.createCDATASection(text) A CDATA section containing the text string text
docObj.createComment(text) A comment node containing the text string text
docObj.createDocumentFragment() An empty document fragment
docObj.createElement(tag) An element node with the tag name tag
docObj.createElementNS(uri, qName) An element node in the uri namespace with the qualified name qName
docObj.createEntityReference(entity) An entity reference named entity
docObj.createProcessingInstruction(target, text) A processing instruction node with a target named target and the text string text
docObj.createTextNode(text) A text node containing the text string text

 

Inserting and removing nodes
Method Description
parent.appendChild(child) Appends the child node child to the parent node parent
nodeObj.cloneNode(deep) Clones the contents of nodeObj; if deep has the Boolean value true, all descendant nodes and their contents are also copied; if deep has the Boolean value false, no descendant nodes are copied
parent.insertBefore(new, old) Inserts the new child node before the old child node
parent.removeChild(nodeObj) Removes nodeObj from the parent node
parent.replaceChild(new, old) Replaces the old child node with the new child node

 

Attribute methods
Method Description
nodeObj.getAttribute(attribute) Returns the value of attribute
nodeObj.getAttributeNS(uri, attribute) Returns the value of attribute within the namespace uri
nodeObj.getAttributeNode(attribute) Returns the attribute node
nodeObj.getAttributeNodeNS(uri, attribute) Returns the attribute node from within the namespace uri
nodeObj.removeAttribute(attribute) Removes the attribute
nodeObj.removeAttributeNS(uri, attribute) Removes the attribute within the samespace uri
nodeObj.removeAttributeNode(attribute) Removes the attribute node
nodeObj.removeAttributeNodeNS(uri, attribute) Removes the attribute node within the namespace uri
nodeObj.setAttribute(attribute, value) Adds new attribute or changes the value of an existing attribute to value
nodeObj.setAttributeNS(uri, attribute, value) Adds or modifies the attribute within the namespace uri
nodeObj.setAttributeNode(attribute) Adds the attribute node
nodeObj.setAttributeNodeNS(uri, attribute) Adds the attribute node within the namespace uri 

 

To create an object to represent the processor, run the following JavaScript command

 

    // to create an object to represent the processor, run the following JavaScript command
    processorObj = templateObj.createProcessor();
    // Example:
    XSLTProc = XSLTemp.createProcessor();
    // where processorObj is a JavaScript object representing the XSLT processor and templateObj is a template object.