Hi all.
I’m not all that confident in Flash but have had to build a search. I’m using “A Flash XML Search Engine type thing, by Dan Tavelli” as my starting point. It utilises XPath functions from Neeld Tanksley (www.xfactorstudio.com) XPath for Actionscript.
I’m having trouble getting my head round a couple of things…
The first thing I’m having trouble with is the search input. If an all lowercase word is input into the search box, it isn’t bringing back results which are all uppercase or title case in the XML document. I can figure out how to change the input search text from uppercase to lowercase, and how to display all of the result text in uppercase or lowercase, but can not for the life of me figure out how to get it to bring back results from the XML document where text is written using different cases.
This is the code in the Flash file…
// imports xfactor xpath functions and creates new XML doc
import com.xfactorstudio.xml.xpath.*;
myDoc = new XML();
myDoc.load("data/content.xml");
// search and replace prototype from actionscript.org
// this is used later on to search for term and replace with same term
// wrapped in html code to highlight
String.prototype.searchReplace = function(find, replace) {
return this.split(find).join(replace);
};
// Part 1 of search (runs when search button is pressed, or the enter key)
function startSearch() {
// clears result text areas
this.taGuestStar.text = "";
this.txDescription.text = "";
this.txTitle.text = "";
this.txDate.text = "";
// gets search word from input text
term = this.taSearchWords.text;
if (term == "") { // if search is blank
this.errorBox.gotoAndStop(2);
}
else { // else continue on with search
// gets data from radio button on where to search.
//The 'data' for each button corresponds to the xml tag name it searches
tag = searchGroup.selection.data;
// builds query string. this searches every 'ep' tag for
// the term from the input text box (term) in the chosen tag (tag) [Description, Guest Star or Title]
var query:String = "//ep[contains("+tag+", "+"'"+term+"'"+")]";
// selects the nodes using contains function of XPath
myResults = XPath.selectNodes(myDoc, query);
// Converts myResults array of xml nodes to regular array
// this part was made with the help of Neeld
myDataSet = new Array();
for (var i = 0; i<myResults.length; i++) {
var name = XPath.selectNodes(myResults*, "./name")[0].firstChild.nodeValue;
var id = XPath.selectNodes(myResults*, "./id")[0].firstChild.nodeValue;
myDataSet.push({Name:name, ID:id});
}
//sends resulting array to result datagrid
this.dgSearchResults.dataProvider = myDataSet;
// my crappy little hit counter /////////////////
var hits = myResults.length;
if (hits == 1) {
hits = "One Match";
} else if (hits>1) {
hits = hits+" Matches Found";
} else {
hits = "No Matches";
}
//sets hit counter label
this.ResultsLabel.text = hits;
////////////////////////////////
}
}
// Part 2 of search (runs when a result is choosen from result datagrid)
function highlightTerms() {
/* the reason for all this nonsense is that the episode descriptions
can be lengthy, so instead of including them in the result array
which would slow down the search I
am using the 'id' from the result array to build
the xml path to the episode selected
If you don't have large blocks of text to return and you don't
need to highlight the search term, you don't neccesarrily need this
step
*/
var gridindex = this.dgSearchResults.getSelectedItem().ID;
var sindex = gridindex.charAt(0); // this is the season number
var epindex1 = gridindex.charAt(2);
var epindex2 = gridindex.charAt(3);
if (epindex1 == 0) {
epindex = epindex2; // this is episode number if <10, so that it ignores the leading '0'
} else {
epindex = epindex1+epindex2; // this is episode number if >10
}
// Uses selected episode id to select the nodes for that episode
// which can then be used to get all the information from it
var indexQuery:String = "/episodeguide/season["+sindex+"]/ep["+epindex+"]";
var choosenEpisodeData = XPath.selectNodes(myDoc, indexQuery);
// gets the search term from input text box tp be used later
var term = this.taSearchWords.text;
// highlights search word in the description
var choosenEpisodeDescription = XPath.selectNodes(choosenEpisodeData, "./d")[0].firstChild.nodeValue;
choosenEpisodeDescription = choosenEpisodeDescription.toString();
var newString1 = choosenEpisodeDescription.searchReplace(term, "<font color='#0000CC'><b>"+term+"</b></font>");
txDescription.text = newString1;
// set name in the gray bar
txTitle.text = this.dgSearchResults.getSelectedItem().Name;
// set date in the gray bar
var choosenDatetext = XPath.selectNodes(choosenEpisodeData, "./dt")[0].firstChild.nodeValue;
choosenDatetext1 = choosenDatetext.toString();
txDate.text = choosenDatetext1;
}
//////////// OTHER STUF ///////////////////////////////////////
///// for enter key press perform search//////
function keyDownFunction() {
if (Key.isDown(Key.ENTER)) {
startSearch();
}
}
var EnterKeyListener = new Object();
EnterKeyListener.onKeyDown = keyDownFunction;
Key.addListener(EnterKeyListener);
stop();
///////////////////////////////////////////////
// styles for components
txDescription.setStyle("marginLeft", "4");
txDescription.setStyle("marginRight", "1");
Any help would be greatly appreciated!
Anne-Marie