A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 XML Filtering for search

  1. #1
    Member
    Join Date
    Apr 2008
    Posts
    97

    AS3 XML Filtering for search

    Hi all,
    I am wondering if anyone has ever worked on a search feature using E4X in ActionScript. I am about to start working on one and don't necessarily want to start from scratch.

  2. #2
    Member
    Join Date
    Apr 2008
    Posts
    97
    OK welll I had to start from scratch, wasn't as hard as I thought, but I do have one problem. My first XMLList, searchResults, builds fine, but a good deal of nodes do not have the the property linkName, so I wanted to filter the CDATA to catch everything.

    If I preform a search for something that has the linkName property, works perfect. If preform a search on I keep getting ReferenceError: Error #1065: Variable content is not defined.

    The XML can be viewed at http://www.vistage.com/studiovi/xml/cdw.xml

    Code:
    //xml processed higher up
    //search term defined by textbox
    
    function processSearchXML(xmlData:XML, searchTerm:String):XMLList
    {
    	
    	var searchResults:XMLList = xmlData.menuItem.*.(hasOwnProperty('@linkName') && @linkName.toLowerCase() == searchTerm);
    	trace(searchResults);
    	var subResults:XMLList = xmlData.menuItem.*.(content.toLowerCase() == searchTerm);
    	trace (subResults + "SFG");
    
    																	   
    	return searchResults;
    }
    Last edited by FlashDevSD; 07-23-2008 at 08:07 PM.

  3. #3
    Member
    Join Date
    Apr 2008
    Posts
    97
    OK so moving forward i found that I can not filter the text of a cretin node if it has child elements, so I am moving all the text into <desc> tags.

    Here is my new search function everything tell i try to build my XMLList subResults
    Code:
    function processSearchXML(xmlData:XML, searchTerm:String):XMLList
    {
    	trace(xmlData.menuItem.content.desc);  //traces correctly
    	var searchData:XMLList = xmlData.*;
    	//trace(searchData);
    	var searchResults:XMLList = searchData.*.(hasOwnProperty('@linkName') && @linkName.toLowerCase() == searchTerm); //traces correclty
    	
      
    	trace(searchData.content.desc);//traces correctly
    
    	var subResults:XMLList = searchData.content.(desc.toLowerCase() == searchTerm);  // gives me an error ReferenceError: Error #1065: Variable desc is not defined.
    	
    		
    	//searchResults += subResults;
    	// added in error handling 
    	trace (searchResults.length() + " records found");
    	if (!searchResults.length())
    	{
    		error_txt.text = "No Matches Found";
    		error_txt.setTextFormat(error_frmt);
    		addChild(error_txt);
    	}else{
    		error_txt.text = "";
    	}
    																	   
    	return searchResults;
    }

  4. #4
    Member
    Join Date
    Apr 2008
    Posts
    97
    It just hit me, why don't I just add a property searchableTerms to any branch I want search enabled. It is limited to exact matches, but if I fill in the searchableTerms good enough, this will do what I need it to do.

    I hope this helps

    Here is a sample of my xml

    Code:
    <menu>
    	<menuItem >
    	<![CDATA[Day 1]]>
    		<content linkName="" url=""  linkType="multi" searchableTerms="i am from , day 1">
    			<desc><![CDATA[I Am From]]></desc>
    			<multiLink url="cdw/day1/iAmFrom.doc"/>
    			<multiLink url="cdw/day1/iAmFrom.pdf"/>
    		</content>
    		
    		<content linkName="" url=""  linkType="multi" searchableTerms="poetic influences , day 1">
    			<desc><![CDATA[Poetic Influences]]></desc>
    			<multiLink url="cdw/day1/influences.doc"/>
    			<multiLink url="cdw/day1/influences.pdf"/>
    		</content>
    		
    		<content linkName="" url="day1/Strengths.ppt"  linkType="dl" searchableTerms="strengths finder , day 1">
    			<desc><![CDATA[Strengths Finder]]></desc>
    		</content>
    		
    		<content linkName="" url=""  linkType="multi" searchableTerms="have do be , day 1">
    			<desc><![CDATA[Have Do Be]]></desc>
    			<multiLink url="cdw/day1/haveDoBe.doc"/>
    			<multiLink url="cdw/day1/haveDoBe.pdf"/>
    		</content>
    	</menuItem>
    </menu>
    Here is the function I used to process the search

    Code:
    //xml loaded above
    function processSearchXML(xmlData:XML, searchTerm:String):XMLList
    {
    	
    	var searchData:XMLList = xmlData.*;
    	
    	//if the node has searchable terms, add it to the list
    	var searchTerms:XMLList = searchData.*.(hasOwnProperty('@searchableTerms'));
    	
    	//A list for the results
    	var searchResults:XMLList = new XMLList();	
    	
    	for each(var searchItem:XML in searchTerms)
    	{
    		//gets the attribute searchableTerms 
    		var searchSplit:String = searchItem.@searchableTerms.toXMLString();
    		
    		//builds the array of " , " seperated values
    		//so that I can have multiple search terms 
    		var searchTerms_ary:Array = searchSplit.split(" , ");
    		//A list for the results
    		var searchResults_ary:Array = new Array();
    		trace(searchTerms_ary.length +" searchable terms in node");
    		
    		//search the array 
    		for each(var term:String in searchTerms_ary)
    		{
    			//if the record matches what I searched for, add it to the results array
    			if (term == searchTerm)
    			{
    				trace(term + " has met the seach craiteria");
    				searchResults_ary.push(term);
    			}
    			
    		}
    		trace(searchResults_ary.length +" results in node");
    		trace();
    		//if anything was added to the array 
    		//add the branch to the xml list 
    		if(searchResults_ary.length != 0)
    		{
    			searchResults += searchItem;
    		}	
    	}
    	  
    	return searchResults;
    }

  5. #5
    Junior Member
    Join Date
    Feb 2009
    Posts
    1

    simple xml data search

    first of all i have a xml file with names and phone numbers. what im trying to do is set up an input text field to put in a name and when you hit submit i want the dynamic text field to show a phone number. im extremely lost and need help. im using flash cs3 as3. dose any have a tutorial on this.


    please please help



    w............

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center