Chapter 3 DynaScript Predefined Objects
The DOMDocument
object inherits
the methods from the DOMNode
object, as well
as the methods discussed in this section.
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. The special
tag name "*" can be used 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 © 1999 Sybase, Inc. All rights reserved. |