Monday, May 4, 2009

XML Extensions for AS 1 and AS2

For those of you using XML and actionscript 1 and 2, I made this set of xml extensions that I use regularly and wanted to pass them along to help you and your projects that involve XML parsing, sending and loading etc.

Here is a quick list of functions and descriptions. Script and docs available for .zip download here.

getRootNode();
returns the root node and all its children(see docs for useage examples)

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

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.


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.


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.


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


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.


findNodesWithAttributeValue([search name],[callBack])

works like findNodes but searches for a value attributed to an attribute anywhere in the XML.

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


Enjoy! If you find any bugs or have ones you'd like to add just comment below! If my script looks lame or you have suggestions, again, comment below, I'm always looking for constructive criticism.

Make it a great week.