XMLExtensions.as Documentation for some Actionscript 1 and 2 XMLNode prototype/class extensions by the Code Handyman (www.codehandyman.com). Compatible with Flash player v5 and up.

For those of you still working in actionscript 1 or 2, you might find these extensions useful. Depending on comments/popularity I may end up writing similar ones for AS3 as necessary.

using include to put this script into any of your flash movies
#include "XMLExtensions.as"

All XML documents will default to "ignoreWhite=true" so whitespaces wont count as nodes. I hated typing that out every time I needed new xml so I just threw this in there.

I will use the following sample XML file for all the methods below:

myXML.xml

to try out the examples, save the above file as myXML.xml in the same folder as some flash movie, then write the following right after the include statement above:

myXML=new XML();
myXML.load("myXML.xml");
myXML.onLoad=function(){

//put samples below here to test them
}

getRootNode ()
returns the root node and all its children

Useage:

myXML=new XML();
myXML.load("myXML.xml");
myXML.onLoad=function(){

//put samples below here to test them
trace(myXML.getRootNode());
//returns
/*
<breakfast><person name="father"><coffee cupColor="white">I Drink Coffee</coffee><plate size="large"><eggs amount="two" /><bacon amount="three" /><hashbrowns amount="1" /></plate></person><person name="mother"><coffee cupColor="white">I Don&apos;t Drink Coffee</coffee><plate size="large"><eggs amount="two" /><bacon amount="one" /><hashbrowns amount="none" /></plate></person><person name="sister"><coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee><plate size="medium"><eggs amount="one" /><bacon amount="none" /><hashbrowns amount="2" /></plate></person><person name="brother"><coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee><plate size="small"><eggs amount="one" /><bacon amount="none" /><hashbrowns amount="1" /></plate></person></breakfast>
*/

}

findNodes([searchName],[callBack Function])
searches all the node names and return them to a callback function as they are found.

Useage:

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
eggsXML = new XML();
eggsXML.parseXML("<allEggs/>");
myXML.findNodes("eggs",addNodesToEggsXML);
trace(eggsXML);
//returns
/*
<allEggs><eggs amount="two" /><eggs amount="two" /><eggs amount="one" /><eggs amount="one" /></allEggs>
*/

};
function addNodesToEggsXML(myNode) {
var myTempNode = new XMLNode();
myTempNode = myNode.cloneNode(true);
eggsXML.firstChild.appendChild(myTempNode);
}

getFirstNodeByName([searchname]);
returns the first node with the specified name in a set of childnodes. Use this when you dont know exactly where the node you need will be in relation to other nodes in the list, but you only need one node and its children for your specific function. You can use a loop to grab many of these as its a quick function that doesnt require a callback function.

Useage:

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
trace(myXML.firstChild.firstChild.getFirstNodeByName("plate"));
}
//returns
/*
<plate size="large"><eggs amount="two" /><bacon amount="three" /><hashbrowns amount="1" /></plate>
*/

getFirstNodeValueByName([search node name])
works as getFirstNodeByName but this one returns the nodevalue. Textnode or not. (I hate that nodes have to be textnodes in order to read the node value so I bypass that using a function called returnNodeValue I'll describe later.

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
trace(myXML.firstChild.getFirstNodeByName("person").getFirstNodeValueByName("coffee"));
}
//returns
/*
I Drink Coffee
*/

returnNodeValue()
The XMLNode docs tell you that a node has to be a type "textNode" or type 3 or you cant use the basic xmlNode.nodeValue to grab the text in a node. Unfortunately loading in XML from anywhere generally is not that type, it all comes in as type 1. So I wrote this function to extract the nodevalue even if the type is not 3.

Useage:

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
trace(myXML.firstChild.getFirstNodeByName("person").getFirstNodeByName("coffee").returnNodeValue());
}
//returns
/*
I Drink Coffee
*/

getTotalNodesByName([searchName]);
This returns the total number of nodes in your xml file that have the specified name. A handy searchy tool.

Useage:

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
trace(myXML.getTotalNodesByName("coffee"));
}
//returns
/*
4
*/

getNextNode([search name],[callBack])
works almost the same as findNodes except it returns an undefined node when its finished. Havent really found a use for this one yet.

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
trace(myXML.getNextNode("coffee", doNext));
};
function doNext(myNode) {
trace(myNode.toString());
}
//returns
/*
<coffee cupColor="white">I Drink Coffee</coffee>
<coffee cupColor="white">I Don&apos;t Drink Coffee</coffee>
<coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee>
<coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee>
undefined
*/

findNodesWithAttributeValue([search name],[callBack])
works like findNodes but searches for a value attributed to an attribute anywhere in the XML.

Useage:

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
myXML.findNodesWithAttributeValue("white", doNext);
};
function doNext(myNode) {
trace(myNode.toString());
}
//returns
/*
<coffee cupColor="white">I Drink Coffee</coffee>
<coffee cupColor="white">I Don&apos;t Drink Coffee</coffee>
*/

findNodesWithAttribute([attribute name to search for])
Works as findNodesWithAttributeValue with just the attribute name

myXML = new XML();
myXML.load("myXML.xml");
myXML.onLoad = function() {
//put samples below here to test them
myXML.findNodesWithAttribute("cupColor", doNext);
};
function doNext(myNode) {
trace(myNode.toString());
}
//returns
/*
<coffee cupColor="white">I Drink Coffee</coffee>
<coffee cupColor="white">I Don&apos;t Drink Coffee</coffee>
<coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee>
<coffee cupColor="pink">I Don&apos;t Drink Coffee</coffee>
*/

--END--