
XML.appendChild() Method
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Alphabetical Language Reference
|
919
currentNode.removeNode();
}
}
}
}
Traditionally, the technique shown in this example is very efficient. However, in Flash
Player 5, the Array.concat() method executes quite slowly. Hence, in Flash 5 it’s quicker to
strip whitespace using the technique shown in the following example. Study the comments
carefully:
// Strip Whitespace Using Function Recursion
// Strips whitespace nodes from an XML document
// by passing twice through each level in the tree.
function stripWhitespaceDoublePass (theNode) {
// Loop through all the 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 j = 0;
var emptyNode = true;
for (j = 0;j < theNode.childNodes[i].nodeValue.length; j++) {
// A useful character is anything over 32 (space, tab,
// new line, etc. are all below 32).
if (theNode.childNodes[i].nodeValue.charCodeAt(j) > 32) {
emptyNode = false;
break;
}
}
// If no useful characters were found, delete the node.
if (emptyNode) {
theNode.childNodes[i].removeNode();
}
}
}
// Now that all the whitespace nodes have been removed from theNode,
// call stripWhitespaceDoublePass()