Articles Comments

Flex Certification » Flex Examples » XMLSearch

XMLSearch

XMLSearch.as

{
public class XmlSearch
{
public function XmlSearch(xml:XMLList)
{
if(xml != null)
{
this.xmlTree = xml;
}
}

public function findNode(lbl:String) : void
{
this.foundNode = false;
resultStr = null;

if(xmlTree != null)
{
for each(var xml:XML in xmlTree)
{
find(xml, lbl);
if(foundNode == true) return;
}
}
}

public function findNodeParent(lbl:String) : void
{
this.foundNode = false;
resultStr = null;

if(xmlTree != null)
{
for each(var xml:XML in xmlTree.children())
{
findParent(xml, lbl);
if(foundNode == true) return;
}
}
}

private var xmlTree:XMLList;
public function set XMLTree(tree:XMLList):void
{
xmlTree = tree;
}
public function get XMLTree(): XMLList
{
return xmlTree;
}

private var resultStr:String;
public function get searchResult():String
{
return resultStr;
}

private var _resultNode:XML = null;
public function get resultNode():XML
{
return _resultNode;
}

private var foundNode:Boolean = false;
public function get nodeFound():Boolean
{
return foundNode;
}

//Find node in the list and set results to that node – used by findNode method
private function find(node:XML, lbl:String) : String
{
var len:int = node.children().length();
var str:String;

str = node.@label;
if(node.@label == lbl)
{
foundNode = true;
_resultNode = node;
resultStr = node.@label;
return resultStr;
}

var children:XMLList = node.children();
var node1:XML;

for each(node1 in children)
{
find(node1, lbl)
}

return null;

}

//Find selected node and set result as parent – used by findNodeParent method
private function findParent(node:XML, lbl:String) : String
{
var len:int = node.children().length();
var str:String;

str = node.@label;
if(node.@label == lbl)
{
foundNode = true;
_resultNode = node;
resultStr = node.parent().@label;
return resultStr;
}

var children:XMLList = node.children();
var node1:XML;

for each(node1 in children)
{
findParent(node1, lbl)
}

return null;

}

}
}

Written by admin

Filed under: Flex Examples

Leave a Reply

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>