Chapter 3 DynaScript Predefined Objects


DOMDocument object

Object

Represents an XML document.

Syntax

To use a DOMDocument's property:

DOMDocument.propertyName

To use a DOMDocument's method:

DOMDocument.MethodName( parameter ) 

Description

The DOMDocument object represents the root of the DOM document node tree, and provides the primary access to the document's data.

You can create a DOMDocument object either by calling the toDOMDocument method, or by invoking the DOMDocument constructor.

DOMDocument objects inherit all of the properties and methods of the DOMNode object as well as the properties and methods defined here.

Dynamo's current implementation of DOM follows the DOM Level 1 specification.

Example

This statement creates a new, empty DOMDocument object:

doc = new DOMDocument()

doctype property

Syntax

DOMDocument.doctype

Description

The DOMDocumentType object representing the document type declaration (DTD) associated with this document. XML documents without a DTD return null. DOM Level 1 does not support editing the DTD.

Example

To return the DTD associated with the parsed XML document domDoc:

dtd = domDoc.doctype

See also

"DOMDocumentType object"

documentElement property

Syntax

DOMDocument.documentElement

Description

Each XML document has a single element that contains all other elements in the document. This is called the root element or the document element. The documentElement method returns the DOMElement object representing the root element of the XML document.

Example

To parse a document held in xmlString, create a variable holding the root element of the document, and write out the name of that element, enter:

domDoc = toDOMDocument( xmlString );
docElement = domDoc.documentElement;
document.writeln( docElement.tagName );

implementation property

Syntax

DOMDocument.implementation

Description

The DOMImplementation object that handles this document. The implementation features can then be retrieved using the hasFeature method of the DOMImplementation object.

Example

This example writes out details of the current implementation:

imp = domDoc.implementation;
document.writeln( "HTML? " + imp.hasFeature( "HTML", "1.0" ) ) ;
document.writeln( "XML? " + imp.hasFeature( "XML", "1.0" ) ) ;

The output from this example is:

HTML? false
XML? true

See also

"DOMImplementation object".

createAttribute method

Syntax

DOMDocument.createAttribute( name )

Description

Creates a DOMAttribute object representing an attribute of the given name.

The created attribute does not have any place in the document. It can be set on an element by using the setAttributeNode method of a DOMElement object, and the value of the attribute can be set using the setAttribute method of a DOMElement object.

name is the name of the attribute for which a DOMAttribute object is being created.

Return

A DOMAttribute object

Example

This example creates an attribute named Region:

domDoc.createAttribute( "Region" );

See also

"DOMAttribute object"

"setAttribute method"

createCDATASection method

Syntax

DOMDocument.createCDATASection( data )

Description

Creates a CDATA node whose value is the specified string. data is the content of the CDATA section.

The created CDATA section does not have any place in the document. It can be added as the child of a node by using the appendChild method of a DOMNode object.

Return

A DOMCDATASection object

Example

This example creates a CDATA section with text "Jane & John Doe", and adds it to the document as the last element before the end of the document element:

xmlString = "Jane & John Doe"
domCDATA = domDoc.createCDATASection( xmlString );
element.appendChild( domCDATA );

See also

"DOMCDATASection object"

"appendChild method"

createComment method

Syntax

DOMDocument.createComment( comment )

Description

Creates a DOMComment object representing a comment containing the specified string. comment should contain the text of the comment only, and should exclude the comment start (<!--) and end (-->) characters.

The DOMComment object can be added as the child of a node in the document using the DOMNode.appendChild method, or one of the other DOMNode methods.

Return

A DOMComment object

Example

This example creates a comment on the document domDoc and adds it to the document as the final element before the end of the document element (docElem):

domComment = domDoc.createComment( 
             "Browsers ignore this string" );
 docElem.appendChild( domComment );

See also

"DOMComment object"

createDocumentFragment method

Syntax

DOMDocument.createDocumentFragment( )

Description

Creates an empty DOMDocumentFragment object. These fragments can be used in copy and paste operations.

Return

A document fragment

Example

To create a document fragment, enter:

domDoc.createDocumentFragment;

See also

"DOMDocumentFragment object"

createElement method

Syntax

DOMDocument.createElement( tagName )

Description

Creates a DOMElement object representing the specified element. tagName is the name of the element type that is to be instantiated. Tag names in XML are case-sensitive.

The element can be added as the child of a node using DOMNode.appendChild method, or one of the other DOMNode methods.

Return

A DOMElement object

Example

This example creates an element named Customer.

domDoc.createElement( "Customer" );

See also

"DOMElement object"

createEntityReference method

Syntax

DOMDocument.createEntityReference( name )

Description

Creates a DOMEntityReference object with the specified name.

Return

A DOMEntityReference object

Example

This example creates a reference to the entity for the less than sign (left angle bracket):

domDoc.createEntityReference( "lt" );

See also

"DOMEntityReference object"

createProcessingInstruction method

Syntax

DOMDocument.createProcessingInstruction( target, data )

Description

Creates a DOMProcessingInstruction object given the specified target and data strings. The parameters are:

Return

A DOMProcessingInstruction object.

Example

This example creates a processing instruction that is a simple form of the required heading for an XML document. It then appends this processing instruction to an empty DOMDocument object:

domDoc = new DOMDocument();
docPI = domDoc.createProcessingInstruction( "xml", "version='1.0'" );
domDoc.appendChild( docPI );

See also

"DOMProcessingInstruction object"

createTextNode method

Syntax

DOMDocument.createTextNode( data )

Description

Creates a DOMText object representing the specified data string.

Return

A DOMText object

Example

This example creates a text node with content "Bartlebooth":

domDoc.createTextNode( "Bartlebooth" )

getElementsByTagName method

Syntax

DOMDocument.getElementsByTagName( tagname  )

Description

Returns a DOMNodeList object of all the DOMElement objects with a given tag name in the order in which they would be encountered in the document. Tag names in XML are case-sensitive. You can use the special tag name "*" to retrieve a list of all DOMElement objects regardless of the tag name.

To search for DOMElement objects that are children of a particular element, use the getElementsByTagName method on the DOMElement object.

Return

A DOMNodeList object containing all the matched objects

Example

This example gets all elements named Region from the document domDoc, and loops over those elements:

elemList = domDoc.getElementsByTagName( "Region" );
for( iElem=0; iElem < elemList.length; iElem++ ){
  elem = elemList.item( iElem );
  -- operations here
}

prettyPrint method

Syntax

DOMDocument.prettyPrint()

Description

This method is not part of the DOM specification, and is provided for debugging purposes.

Return

A string containing a representation of the document structure

Example

To print out the entire structure of a document named domDoc, enter:

document.writeln( domDoc.prettyPrint() );

 


Copyright © 2001 Sybase, Inc. All rights reserved.