Chapter 3 DynaScript Predefined Objects
The DOMElement
object inherits
all of the methods of the DOMNode
object, but it
also has its own methods, which are discussed in this section.
DOMElement.getAttribute( name )
Retrieves an attribute value by name.
The attribute value as a string or an empty string if the attribute does not have value.
This example writes out the value of the Type attribute for each Region element in a DOM document stored in the variable domDoc.
elemlist = domDoc.getElementsByTagName( "Region" );
for ( iEl = 0 ; iEl < elemlist.length ; iEl++ ){
elem = elemlist.item(iEl);
document.writeln( elem.getAttribute( "Type" ) );
}
DOMElement.getAttributeNode( name )
Retrieves an attribute node by name.
A DOMAttribute
object
or null if there is no such attribute.
This example assigns a DOMAttribute
object
to a variable named attnode. This object represents
the Type attribute for a Region element
in the DOM document domDoc.
elemlist = domDoc.getElementsByTagName( "Region" );
for ( iEl = 0 ; iEl < elemlist.length ; iEl++ ){
elem = elemlist.item(iEl);
attnode = elem.getAttributeNode( "Type" );
document.writeln( attnode.nodeValue );
}
DOMElement.getElementsByTagName( name )
Returns a DOMNodeList
object
of all descendant elements 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 DOMElement
objects
regardless of the tag name.
To search for DOMElement
objects
in the entire document, use the getElementsByTagName
method
on the DOMDocument
object.
A list of matching Element nodes.
Here is a simple XML document with a nested structure:
<?xml version="1.0"?>
<List>
<ListItem>Item 1
<List><ListItem>Item 1.1</ListItem>
</List>
</ListItem>
<ListItem>Item 2</ListItem>
</List>
This script fragment writes out the text content of each ListItem element, in the order in which they are encountered.
var docElem = domDoc.documentElement;
elemlist = docElem.getElementsByTagName( "ListItem" );
document.writeln( elemlist.length) ;
for( iEl=0; iEl < elemlist.length ; iEl++ ){
document.writeln(
elemlist.item(iEl).firstChild.nodeValue );
}
The output from this example is:
Item 1
Item 1.1
Item 2
DOMElement.normalize( )
Normalizing an element places it and all its
descendants into a standard format. For all DOMText
node
objects that are descendants of this element, adjacent (sibling) DOMText
objects
are combined into a single DOMText
object.
Adjacent DOMCDATASection
nodes
are not combined even though they inherit from DOMText
.
This statement normalizes the element named elem.
elem.normalize();
DOMElement.removeAttribute( att_name )
Removes the specified attribute. att_name is the name of the attribute to be removed.
The method returns nothing, as required by
the DOM specification. This means there is no natural way to check
for the success of the method. You can, however while debugging
and testing, execute a prettyPrint()
on
the element after the call.
This method returns nothing.
This function removes all Type attributes from
Region elements in a DOMDocument
object
stored in domDoc.
function RemoveTypeAttribute( domDoc ) {
elemlist = domDoc.getElementsByTagName( "Region" );
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
elem.removeAttribute( "Type" );
}
}
DOMElement.removeAttributeNode( att_node )
Removes the specified attribute node.
The DOMAttribute
object
that was removed.
This example removes all Type attributes from
Region elements in a DOMDocument
object
stored in domDoc. It confirms the removal by
writing out the name of the removed attribute.
function RemoveTypeAttribute( domDoc ) {
elemlist = domDoc.getElementsByTagName( "Region" );
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
attnode = elem.getAttributeNode( "Type" )
old_att = elem.removeAttributeNode( attnode );
document.writeln( "Attribute " + old_att.name +
"removed" );
}
}
DOMElement.setAttribute( name, value )
Adds a new attribute to the element. If an attribute with that name is already present in the element, its value is changed to be that of the value parameter. The parameters are:
To assign an attribute value that contains
entity references, first create a DOMAttribute
object,
plus any DOMText
and DOMEntityReference
objects.
Then insert the text and entity reference objects as children of
the DOMAttribute
, and use setAttributeNode
to
assign the attribute to the element.
This function changes the value of the Type attribute for all Region elements from its current setting to County.
function SetAttribute( domDoc ){
elemlist = domDoc.getElementsByTagName( "Region" );
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
elem.setAttribute( "Type", "County" )
document.writeln( elem.prettyPrint() );
}
}
DOMElement.setAttributeNode( newAttr )
Adds a new attribute. If an attribute with
that name is already present in the element, it is replaced by the
new one. newAttr is the DOMAttribute
object representing
the attribute to be set.
If the newAttr attribute
replaces an existing attribute with the same name, the previously
existing DOMAttribute
object
is returned, otherwise null is returned.
This function resets the value of the Type
attribute for reach Region element to County. It then uses prettyPrint
to
display the structure of the document for debugging purposes.
function SetAttributeNode( domDoc ){
elemlist = domDoc.getElementsByTagName( "Region" );
for( iElem=0; iElem < elemlist.length; iElem++ ) {
elem = elemlist.item( iElem );
attnode = elem.getAttributeNode( "Type" );
attnode.value = "County";
old_att = elem.setAttributeNode( attnode );
}
document.writeln( domDoc.prettyPrint() );
}
Copyright © 1999 Sybase, Inc. All rights reserved. |