Actionscript XML Search Utillity

A neat little class for searching through an xml object, options include recursive search or just return the first instance. Proper documentation to follow.

/*
XmlSearch v.1.1
Author:Max Garfinkel
Last Revision: 05/07/2007
new methods added
 
Finds and returns attributes and node values from an xml object
 
public function are:
 
matchAttribute(xml:XML, attributeName:String, attributeValue:String):Boolean
returns true if there exists an attribute of attribute name with the value of attribute value in the xml.
 
getAttributes(xml:XML, searchterm:String, attributeName:String, recursive:Boolean):Array
returns array of 0-* elements containing the attributes attributeName, of node searchterm.
If recursive flag is set to false array will be of 0-1 length.
 
getNodeContent(xml:XML, searchterm:String, recursive:Boolean):Array
returns an array of 0-* elements containing the contents of node searchterm.
If recursive flag is set to false the array will be of 0-1 length.
 
getNodeContentXML(xml:XML, searchterm:String, recursive:Boolean):Array
returns an array of 0-* elements containing the contents of node searchterm.
If recursive flag is set to false the array will be of 0-1 length.
The data is returned as xml blocks within the array.
 
*/
 
class com.maxgarfinkel.data.XmlSearch
{
	private var returnData:Array;
 
		public XmlSearch(){}
 
		//checks if nodes attribute matches filter value
		public function matchAttribute(xml:XML, attributeName:String, attributeValue:String):Boolean
		{
			if(xml.attributes[attributeName].toString() == attributeValue)
				return true;
			else
				return false;
		}
 
        public function getAttributes(xml:XML, searchterm:String, attributeName:String, recursive:Boolean):Array
        {
                this.returnData = [];
                this.searchId(xml, searchterm, attributeName, recursive);
                return(returnData);
        }
 
        private function searchId(xml:XML, searchterm:String, attributeName:String, recursive:Boolean):Void
        {
                for(var i:Number = 0; i < xml.childNodes.length; i++)
                {
                        if(xml.childNodes[i].nodeName.toString() == searchterm)
                                this.returnData.push(xml.childNodes[i].attributes[attributeName]);
                        if(recursive == true)
                                this.searchId(xml.childNodes[i], searchterm, attributeName, recursive);
                }
        }
 
        //Returns xml node data as elements in a single array
        public function getNodeContent(xml:XML, searchterm:String, recursive:Boolean):Array
        {
		this.returnData = [];
                this.searchContent(xml, searchterm, recursive);
                return(returnData);
        }
	//Returns xml node data as xml elements in a single array
	public function getNodeContentXML(xml:XML, searchterm:String, recursive:Boolean):Array
        {
		this.returnData = [];
                this.searchContentReturnXml(xml, searchterm, recursive);
                return(returnData);
        }
 
        private function searchContent(xml:XML, searchterm:String, recursive:Boolean):Void
        {
                for(var i:Number = 0; i < xml.childNodes.length; i++)
                {
                        if(xml.childNodes[i].nodeName.toString() == searchterm)
				this.returnData.push(xml.childNodes[i].firstChild.nodeValue);
                        if(recursive == true)
                                this.searchContent(xml.childNodes[i], searchterm, recursive);
                }
        }
 
	private function searchContentReturnXml(xml:XML, searchterm:String, recursive:Boolean):Void
        {
                 for(var i:Number = 0; i < xml.childNodes.length; i++)
                {
                        if(xml.childNodes[i].nodeName.toString() == searchterm)
				this.returnData.push(xml.childNodes[i]);
                        if(recursive == true)
				this.searchContentReturnXml(xml.childNodes[i], searchterm, recursive);
                }
        }
}

download the as file here


About this entry