Table of Contents

XML.nodeType Property Flash 5

the type of the current node read-only
theNode.nodeType

Description

The nodeType is an integer property that returns theNode's type (theNode can be an XML or XMLnode instance). Since only two node types are supported by ActionScript�element nodes and text nodesnodeName has only two possible values: 1, if the node is an element node; and 3, if the node is a text node. These values may seem arbitrary, but actually they are the appropriate values as stipulated by the DOM. For reference, the other node types in the DOM are listed in Table 18-26.

Table 18-26. DOM node types

Node description

Node type code

ELEMENT_NODE[12]

1

ATTRIBUTE_NODE

2

TEXT_NODE[12]

3

CDATA_SECTION_NODE

4

ENTITY_REFERENCE_NODE

5

ENTITY_NODE

6

PROCESSING_INSTRUCTION_NODE

7

COMMENT_NODE

8

DOCUMENT_NODE

9

DOCUMENT_TYPE_NODE

10

DOCUMENT_FRAGMENT_NODE

11

NOTATION_NODE

12

[12] Supported by Flash.

Technically, ActionScript implements so-called attribute, document, and document_type nodes in addition to element and text nodes, but we don't have direct access to them as objects. For example, we can manipulate the attributes of a node through the attributes property, but we do not have direct access to attribute nodes themselves. Similarly, we have access to the DOCTYPE tag of a document through the docTypeDecl property, but we do not have direct access to the document_type node itself.

Element nodes correspond to XML or HTML tags. For example, in the XML fragment <P>what is your favorite color?</P>, the P tag is represented in an XML object hierarchy as an element node (nodeType 1). The text contained by a tag in XML source code�for example, the text "what is your favorite color?"�is represented as a text node (nodeType 3). CDATA section nodes are irreversibly converted to text nodes when they are parsed, as described under XML.parseXML( ).

Example

We can operate on a node conditionally, based on its nodeType. For example, here we remove all the empty text nodes that are children of theNode:

// Loop through all children of theNode
for (var i=0; i < theNode.childNodes.length; i++) {
  // If the current node is a text node...
  if (theNode.childNodes[i].nodeType =  = 3) {
    // Check for any useful characters in the node
    var emptyNode = true;
    for (var j=0; j < theNode.childNodes[i].nodeValue.length; j++) {
      // Useful character codes start above ASCII 32
      if (theNode.childNodes[i].nodeValue.charCodeAt(j) > 32) {
        emptyNode = false;
        break;
      }
    }
    if (emptyNode) {
      // No useful characters were found, so delete the node
      theNode.childNodes[i].removeNode();
    }
  }
}

See Also

The XML class, XMLnode.attributes, XML.docTypeDecl, XML.nodeName, XML.nodeValue


Table of Contents