Chapter 3 DynaScript Predefined Objects
Represents an XML document.
To use a DOMDocument's property:
DOMDocument.propertyName
To use a DOMDocument's method:
DOMDocument.MethodName( parameter )
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.
toDOMDocument
parses
an existing XML document and returns a DOMDocument
object
representing the document contents.
DOMDocument
constructor
creates an empty DOMDocument
object.
The node object creation methods on the DOMDocument
can
then be called to create new node objects. The created DOMNode
objects
have an ownerDocument
property
which associates them with the DOMDocument
within
whose context they were created.
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.
This statement creates a new, empty DOMDocument
object:
doc = new DOMDocument()
DOMDocument.doctype
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.
To return the DTD associated with the parsed XML document domDoc:
dtd = domDoc.doctype
DOMDocument.documentElement
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.
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 );
DOMDocument.implementation
The DOMImplementation
object
that handles this document. The implementation features can then
be retrieved using the hasFeature
method of
the DOMImplementation
object.
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
DOMDocument.createAttribute( name )
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.
A DOMAttribute
object
This example creates an attribute named Region:
domDoc.createAttribute( "Region" );
DOMDocument.createCDATASection( data )
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.
A DOMCDATASection
object
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 );
DOMDocument.createComment( comment )
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.
A DOMComment
object
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 );
DOMDocument.createDocumentFragment( )
Creates an empty DOMDocumentFragment
object.
These fragments can be used in copy and paste operations.
A document fragment
To create a document fragment, enter:
domDoc.createDocumentFragment;
DOMDocument.createElement( tagName )
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.
A DOMElement
object
This example creates an element named Customer.
domDoc.createElement( "Customer" );
DOMDocument.createEntityReference( name )
Creates a DOMEntityReference
object
with the specified name.
A DOMEntityReference
object
This example creates a reference to the entity for the less than sign (left angle bracket):
domDoc.createEntityReference( "lt" );
DOMDocument.createProcessingInstruction( target, data )
Creates a DOMProcessingInstruction
object
given the specified target and data strings. The parameters are:
A DOMProcessingInstruction
object.
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 );
"DOMProcessingInstruction object"
DOMDocument.createTextNode( data )
Creates a DOMText
object
representing the specified data string.
A DOMText
object
This example creates a text node with content "Bartlebooth":
domDoc.createTextNode( "Bartlebooth" )
DOMDocument.getElementsByTagName( tagname )
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.
A DOMNodeList
object
containing all the matched objects
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 }
DOMDocument.prettyPrint()
This method is not part of the DOM specification, and is provided for debugging purposes.
A string containing a representation of the document structure
To print out the entire structure of a document named domDoc, enter:
document.writeln( domDoc.prettyPrint() );
Copyright © 2001 Sybase, Inc. All rights reserved. |
![]() |