Chapter 3 DynaScript Predefined Objects
Represents character data in an XML document.
To use a DOMCharacterData's property:
DOMCharacterData.propertyName
To use a DOMCharacterData's method:
DOMCharacterData.MethodName( parameter )
The DOMCharacterDATA
object
inherits all the methods and properties of the DOMNode
object
as well as providing its own set of methods and properties to access
and manipulate character data in the DOM.
DOMCharacterData.data
The text represented by this object.
This DynaScript fragment writes out the character data associated with all text node children of an element:
child = elem.firstChild; do { if( child.nodeType == 3 ){// text node document.writeln( child.data ); child = child.nextSibling; } } while ( child != null )
DOMCharacterData.length
The number of characters in the text representing by this object. The length is an integer value greater than or equal to zero.
This DynaScript fragment writes out the length
of each text node child of a DOMElement
object
named elem:
child = elem.firstChild; do { if( child.nodeType == 3 ){// text node document.writeln( child.data ); child = child.nextSibling; } } while ( child != null )
DOMCharacterData.appendData( DOMString )
Appends the string to the end of the character data represented by this object. DOMString is the string that is to be appended.
Here is a very simple document:
<?xml version='1.0' encoding='ISO-8859-1' ?> <Customer> <FName>Jessie</FName> <LName>Gagliardo</LName> <Address><Street>2800 Park Avenue</Street> <City>Hull</City> <Region Type="Province">PQ</Region> </Address></Customer>
This DynaScript fragment, which includes a Region element, checks whether the region is a province or a state, and appends a string indicating the country to the text element.
child = elemRegion.firstChild; if( elemRegion.getAttribute( "Type" ) == "Province" ){ child.appendData( ", Canada" ); } else { child.appendData( ", USA" ); } document.writeln( child.data );
DOMCharacterData.deleteData( offset, count )
Removes a range of characters from the text represented by this object. The parameters are:
to
the end of the data are deleted.
This DynaScript statement deletes the first two characters from a text node:
if( elem.nodeType == 3 ) { // text node elem.deleteData( 0, 2 ) }
DOMCharacterData.insertData( offset, DOMString )
Inserts a string at the specified character offset. The parameters are:
This DynaScript fragment inserts a string at the beginning of a text node:
if( elem.nodeType == 3 ) { // text node elem.insertData( 0, "Region: " ) }
DOMCharacterData.replaceData( offset, count, DOMString )
Replaces the character starting at the specified character offset with the specified string. The parameters are:
This DynaScript fragment replaces the third and fourth characters in a text node with the string "XXX":
if( elem.nodeType == 3 ) { // text node elem.replaceData( 3, 2, "XXX" ) }
DOMCharacterData.substringData( offset, count )
Extracts a portion of the text represented by this object. The parameters are:
A string containing the extracted portion of the text
This DynaScript fragment writes out the characters from the fourth to the hundredth of all text nodes that are children of a Street element:
function SubstringDataCharacterData( domDoc ) { var elemlist = domDoc.getElementsByTagName( "Street" ) for( iElem=0; iElem < elemlist.length; iElem++ ) { var elem = elemlist.item( iElem ); var child = elem.firstChild; do { if( child.nodeType == 3 ){ // text node var svar = child.substringData( 3, 99 ); document.writeln( "svar = " + svar ); } child = child.nextSibling; } while ( child != null ) } }
Copyright © 2001 Sybase, Inc. All rights reserved. |
![]() |