Chapter 3 DynaScript Predefined Objects


DOMCharacterData object

Object

Represents character data in an XML document.

Syntax

To use a DOMCharacterData's property:

DOMCharacterData.propertyName

To use a DOMCharacterData's method:

DOMCharacterData.MethodName( parameter ) 

Description

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.

data property

Syntax

DOMCharacterData.data

Description

The text represented by this object.

Example

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 )

length property

Syntax

DOMCharacterData.length

Description

The number of characters in the text representing by this object. The length is an integer value greater than or equal to zero.

Example

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 )

appendData method

Syntax

DOMCharacterData.appendData( DOMString )

Description

Appends the string to the end of the character data represented by this object. DOMString is the string that is to be appended.

Example

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 );

deleteData method

Syntax

DOMCharacterData.deleteData( offset, count )

Description

Removes a range of characters from the text represented by this object. The parameters are:

Example

This DynaScript statement deletes the first two characters from a text node:

if( elem.nodeType == 3 ) { // text node
  elem.deleteData( 0, 2 )
}

insertData method

Syntax

DOMCharacterData.insertData( offset, DOMString )

Description

Inserts a string at the specified character offset. The parameters are:

Example

This DynaScript fragment inserts a string at the beginning of a text node:

if( elem.nodeType == 3 ) { // text node
  elem.insertData( 0, "Region: " )
}

replaceData method

Syntax

DOMCharacterData.replaceData( offset, count, DOMString )

Description

Replaces the character starting at the specified character offset with the specified string. The parameters are:

Example

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" )
}

substringData method

Syntax

DOMCharacterData.substringData( offset, count )

Description

Extracts a portion of the text represented by this object. The parameters are:

Return

A string containing the extracted portion of the text

Example

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.