xml search class
A neat little class that allows you to search an already loaded xml file. You can search for attributes or node data and you can search for the first match only or you can search recursivly.
http://blog.maxgarfinkel.com/archives/8
3 Comments »
RSS feed for comments on this post.
| TrackBack URI
You can also bookmark
this on del.icio.us or check the cosmos
Leave a comment
« AS3 Embeded
HTMLStyle »
Hi there, thanks for your little class, i’m sure i’ll use it very often.
The only thing is that i want to make sure i use it the right way, since i dont have extensive experience using flash and actionscript. I’m more used to php.
So here’s my code (doesnt work actually). If you could guide me with tips i sure would be very grateful.
So beforehand, i’ve created the path “classLib/com/maxgarfinkel/data” and have stored your XmlSearch.as file there.
After that, i’ve set the right classpath in flash in order to be able to have access to it.
Should i use #include instead of import?
import com.maxgarfinkel.data.XmlSearch;
xmlData = new XmlSearch();
xDoc = new XML();
xDoc.ignoreWhite = true;
xDoc.load(”test.xml”);
xSearch = new Array;
xSearch = xmlData.getAttributes(”test.xml”, “season2″, “path”, recursive);
trace(xSearch);
thank you so much for your time…
Comment by jeanmichel — June 28, 2007 @ 1:59 pm
Shame the download link is broken. This looks really handy.
Comment by James Wagstaff — May 29, 2009 @ 9:54 am
/*
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);
}
}
}
Comment by asd — June 23, 2009 @ 2:54 am