The DOMDocument Object

The DOMDocument Object

The DOMDocument object is the main object you work with, and it represents the top node in every document tree. When working with the DOM, this is the only object you create directly.

As we'll see in this chapter, there are two ways to create document objects in Internet Explorer: using the Microsoft.XMLDOM class and using XML data islands. Creating a document object with the Microsoft.XMLDOM class looks like thisyou explicitly load a document into the object with the load method:

 function readXMLDocument()  {     var xmldoc  xmldoc = new ActiveXObject("Microsoft.XMLDOM")  xmldoc.load("ch07_01.xml")    .    .    . 

Microsoft.XMLDOM represents a fairly early version of Microsoft's XML (MSXML) support, but it also represents a common denominator for that support. You can specify that you want to use a later version of the MSXML package like this, which explicitly uses version 4.0 (you can also use "MSXML2.DOMDocument.2.0" or "MSXML2.DOMDocument.3.0" ):

 function readXMLDocument()     var xmldoc  xmldoc = new ActiveXObject("MSXML2.DOMDocument.4.0")  xmldoc.load("ch07_01.xml")    .    .    . 

The problem with this is that not all users will have MSXML 4.0 installed. Of course, if you need the advanced functionality of a recent version of MSXML (such as the capability to work with full XML schemas with MSXML 4.0, as we saw in Chapter 5, "Creating XML Schemas"), you don't have any other options.

We'll also see that you can use the <XML> HTML element to create a data island in Internet Explorer and then use the XMLDocument property of that element to gain access to the corresponding document object:

 <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>  <SCRIPT LANGUAGE="JavaScript">     function readXMLDocument()     {          xmldoc = document.all("meetingsXML").XMLDocument          .          .          . 

Here is the base set of properties for this object. (MSXML version 4.0 will have more properties, for example, as you can see in the documentation that comes with version 4.0.) Throughout this chapter, those items marked with an asterisk (*) represent a Microsoft extension to the W3C DOM:

  • async * Indicates whether asynchronous download is allowed. Read/write.

  • attributes Holds the list of attributes for this node. Read-only.

  • baseName * Is the base name qualified with the namespace. Read-only.

  • childNodes Holds a node list containing child nodes for nodes that may have children. Read-only.

  • dataType * Gives the data type for this node. Read/write.

  • definition * Gives the definition of the node in the DTD or schema. Read-only.

  • doctype Is the document type node, which is what specifies the DTD for this document. Read-only.

  • documentElement Specifies the root element of the document. Read/write.

  • firstChild Is the first child of the current node. Read-only.

  • implementation Is the XMLDOMImplementation object for this document. Read-only.

  • lastChild Is the last child node of the current node. Read-only.

  • namespaceURI * Gives the uniform resource identifier (URI) for the namespace. Read-only.

  • nextSibling Is the next sibling of the current node. Read-only.

  • nodeName Is the qualified name of the element, attribute, or entity reference. Holds a fixed string for other node types. Read-only.

  • nodeType Specifies the XML DOM node type. Read-only.

  • nodeTypedValue * Holds this node's value. Read/write.

  • nodeTypeString * Gives the node type expressed as a string. Read-only.

  • nodeValue Gives the text associated with the node. Read/write.

  • ondataavailable * Is the event handler for the ondataavailable event. Read/write.

  • onreadystatechange * Is the event handler that handles readyState property changes. Read/write.

  • ontransformnode * Is the event handler for the ontransformnode event. Read/write.

  • ownerDocument Specifies the root of the document that contains this node. Read-only.

  • parentNode Is the parent node (for nodes that can have parents). Read-only.

  • parsed * Is true if this node and all descendants have been parsed; is false otherwise . Read-only.

  • parseError * Is an XMLDOMParseError object with information about the most recent parsing error. Read-only.

  • prefix * Is the namespace prefix. Read-only.

  • preserveWhiteSpace * Is true if processing should preserve whitespace; is false otherwise. Read/write.

  • previousSibling Is the previous sibling of this node. Read-only.

  • readyState * Specifies the current state of the XML document. Read-only.

  • resolveExternals * Indicates whether external definitions are to be resolved at parse time. Read/write.

  • given * Indicates whether the node is explicitly given or derived from a default value. Read-only.

  • text * Holds the text content of the node and its subtrees. Read/write.

  • url * Gives the canonicalized uniform resource locator (URL) for the most recently loaded XML document. Read-only.

  • validateOnParse * Indicates whether the parser should validate this document. Read/write.

  • xml * Gives the XML representation of the node and all its descendants. Read-only.

Here is the base set of methods of the document object (as before, this is the base set; MSXML versions such as 4.0 will have more methods). Again, those items marked with an asterisk (*) represent a Microsoft extension to the W3C DOM:

  • abort * Aborts an asynchronous download

  • appendChild Appends a new child as the last child of the current node

  • cloneNode Returns a new node that is a copy of this node

  • createAttribute Returns a new attribute with the given name

  • createCDATASection Returns a CDATA section node that contains the given data

  • createComment Returns a comment node

  • createDocumentFragment Returns an empty DocumentFragment object

  • createElement Returns an element node using the given name

  • createEntityReference Returns a new EntityReference object

  • createNode * Returns a node using the given type, name, and namespace

  • createProcessingInstruction Returns a processing instruction node

  • createTextNode Returns a text node that contains the given data

  • getElementsByTagName Yields a collection of elements that have the given name

  • hasChildNodes Is true if this node has children

  • insertBefore Inserts a child node to the before the given node

  • load * Loads an XML document from the given location

  • loadXML * Loads an XML document using the given string

  • nodeFromID * Yields the node whose ID attribute matches the given value

  • removeChild Removes the given child node from the list of children

  • replaceChild Replaces the given child node with the given new child node

  • save * Saves an XML document to the given location

  • selectNodes * Applies the given pattern-matching operation to this node's context, returning a list of matching nodes

  • selectSingleNode * Applies the given pattern-matching operation to this node's context, returning the first matching node

  • transformNode * Transforms this node and its children using the given XSL stylesheet

  • transformNodeToObject * Transforms this node and its children using the given XSL stylesheet and returns the an object

Here is the base set of the events of the document object:

  • ondataavailable * Indicates that XML document data is available

  • onreadystatechange * Indicates when the readyState property changes

  • ontransformnode * Happens before each node in the stylesheet is applied in the XML source



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net