XML Flash Actionscript Tools
A while ago helped out a designer (Sacha Jerrems) with some ActionScript XML functions that helped him retrieve nodes and node values instead of using firstChild etc.
Instead of using for loops to locate child nodes, and then go into them, we can make simple function calls to do the work for us.
Example Use
Say we have a valid XHTML document, with a node named ‘title’. To get its value we could simple call:
trace(getValue(getNode('title', xhtmlDoc)));
This method allows us to pipe the results without needing to assign them to variables etc. It can typically compress your code from 10s of lines down to a single assignment.
The Actionscript
/**
* Searches through an XMLNode's children to locate a particular child node
*
* @param String nodeName The nodeName to locate
* @param XMLNode parent The parent to search through
* @returns Found node or null if it could unable to locate a match
* @author Cameron Manderson <cameronmanderson@gmail.com>
*/
function getChild(nodeName, parent) {
return getNode(nodeName, parent, false);
}
/**
* Searches Recursively through an XMLNode to locate a particular
* child node
*
* @param String nodeName The nodeName to locate
* @param XMLNode parent The parent to search through
* @param boolean recursive Whether to search recrusively
* @returns Found node or null if it could unable to locate a match
* @author Cameron Manderson <cameronmanderson@gmail.com>
*/
function getNode(nodeName, parent, recursive) {
// Check that we are not looking through or for undefined variables
if(nodeName == undefined || parent == undefined) {
return null;
}
if(recursive == undefined) recursive = false;
var i = 0; var locatedNode = null;
// Check parent for match
if(parent.nodeName == nodeName) {
return parent;
}
// Check through the child nodes until the particular child node is located
for(var i = 0; parent.childNodes && i < parent.childNodes.length; i++) {
// look to see if the child is what we want
if(parent.childNodes[i].nodeName == nodeName) {
return parent.childNodes[i];
}
// if we are recursing, recurse into child
if(recursive) {
locatedNode = getChild(nodeName, parent.childNodes[i], recursive);
}
if(locatedNode !== null) {
return locatedNode;
}
}
return null;
}
You could also then use a simple getValue() function such as below to help you retrieve a node value.
function getValue(node) {
if (node && node.firstChild) {
return node.firstChild.nodeValue;
}
return "";
}
Hopefully this helps you quickly find the power of XML with Flash
P.S. I request that you leave my name in the functions if you use them. Code is released under our standard LGPL. Which means it is free for you to use, but you must leave the code intact. If you make changes to the code, you should email them back to me. I wrote this a while back and it is probably time for me to make it AS2, but atleast it is more backward compatible.






These node browser has helped out a lot, any suggestions on ways to extend them would be appreciated.
Cheers
cam
Comment by Cameron Manderson — February 28, 2007 @ 10:28 pm