• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 527
  • Last Modified:

Search large xml file in Flash, as2, with CD-ROM compatibility?

I'm fairly new to actionscripting.  Trying to create a Flash Projector file that will search through all of an xml file's nodes for a search term entered by the user.  The code I have so far is based on a kirupa.com tutorial I found here http://www.kirupa.com/web/xml/examples/searchbestof.htm.  It works fine when I try to search for a word that isn't used much, such as a person's name, but when i test a word that is used frequently, like "for", flash gives me a prompt that a script is making it run slow, computer could blow up, etc...  

It seems like a user was trying to do something similar here http://www.experts-exchange.com/Programming/Misc/Q_21324393.html?sfQueryTermInfo=1+10+file+flash+larg+search+xml and figured how to split up the xml file, i dont know how this is done/if this is done on the fly?  I need one search to be able to return all results found.  Does anyone have code to do this?  I also saw somewhere a solution where you could parse a few nodes, remove them from the array, and then re-enter the frame, as to break up the parsing.  I dont know if that would work in this situation. Sucks to be a noob.  The xml file is c. 614kb, formatted as shown below, with about 350 posts to parse through, which is why Flash is choking.

Also on the agenda is being able to search for a word with case-insensitivity.  But i'll save that, as this issue is more pressing.    

Thanks to anyone who can help me.
//partial code
 
SearchXML = function(nodes, query, useChildElements){
	var results = [];
	for (var i=0; i < 6; i++){
		for (var j=0; j<nodes[i].childNodes.length; j++){
			currNode = nodes[i].childNodes[j];
			if (useChildElements.contains(currNode.nodeName)){
				if (currNode.firstChild.nodeValue.contains(query)){
					results.push(nodes[i]);
					break;
				}
			}
		}
	}
	return results;
}
 
ElementsToSearch = function(){
	var childElementsToSearch = [];
	if (search_fields.title_check.checked){
		childElementsToSearch.push("title");
	}
	if (search_fields.author_check.checked){
		childElementsToSearch.push("author");
	}
	if (search_fields.message_check.checked){
		childElementsToSearch.push("contentText");
	}
	return childElementsToSearch;
}
 
DisplayNodes = function(nodes, field_txt){
	field_txt.htmlText = "";
	var entry;
	var separator = "<br>_______________________<br><br>";
	for (var i=0; i<nodes.length; i++){
		entry = "";
		entry += "<b>"+ nodes[i].childNodes[0].firstChild.nodeValue +"</b><br />";
		entry += nodes[i].childNodes[1].firstChild.nodeValue +"<br />";
		entry += "<i>"+ nodes[i].childNodes[2].firstChild.nodeValue +"</i><br /><br />";
		entry += nodes[i].childNodes[3].firstChild.nodeValue;
		field_txt.htmlText += entry + separator;
		scrollCheck();
			
	}
}
var posts_xml = new XML();
posts_xml.ignoreWhite = true;
posts_xml.onLoad = function(success){
	if (success){
		search_fields._visible = true;
	}else field_txt.text = "Error loading XML";
}
search_fields._visible = false;
posts_xml.load("basicscience.xml");
 
search_fields.title_check.title_txt.text = "Title";
search_fields.author_check.title_txt.text = "Author";
search_fields.message_check.title_txt.text = "Message";
 
search_fields.search_btn.onRelease = function(){
	if (search_fields.query_txt.text.length < 3){
		field_txt.text = "Please use a search term with 3 or more characters.";
		return (0);
	}
	var searchElements = ElementsToSearch();
	var nodesWithQuery =	SearchXML(
		posts_xml.firstChild.childNodes,
		search_fields.query_txt.text,
		searchElements
		);
	
	if (nodesWithQuery.length){
		DisplayNodes(
			nodesWithQuery,
			field_txt
		);
	}else{
		field_txt.text = "No results for "+search_fields.query_txt.text+".";
		return (0);
	}
 
//xml sample
 
<category>
<post>
<author>Ahmad K, Pervaiz S. </author>
<title>Vitamin C dramatically increases photo-oxidation of merocyanine-540 to yield a ROS-dependent intermediate with potent antitumor activity. </title>
<contactInfo>Kashif Ahmad, Northwestern Health Sciences University, 2501 W 84th St, Bloomington, MN 55431.  &#10;kahmad@nwhealth.edu </contactInfo>
<contentText> &#10;3019 &#10;We have shown that photo-oxidation of merocyanine-540 (MC540) yields biologically active photoproducts that induce apoptosis in a variety of tumor cells. (Pervaiz S, et al. Blood. 1999;93[12]:4096-4108). Where...... </contentText>
</post>
</category>

Open in new window

0
dojoHobo
Asked:
dojoHobo
  • 4
  • 2
1 Solution
 
wal_toorCommented:
Hellow dojo,

I am trying to recreate your problem. I have created a xml file and a search box. But it does'nt find any matches at all. Does your posted snipped misses a .contains function?

I was thinking to split up your search in chunks of 100, so the search does not pars the entyre xml at once, but in bits of 100 posts.

greetz,
walter
0
 
dojoHoboAuthor Commented:
Hey walter,
0
 
dojoHoboAuthor Commented:
Ugh, i did neglect to include two function necessary for the search.  How annoying of me.  I appreciate you getting back to me, i would love to split up the xml file like that.  I included the code as it should be, and also attached a txt file of what the xml will be like, sans CDATA tags etc.  The posts are much longer than my example in the first code snippet.  You can see (hopefully)  if you do a search for something like the word "science" the results pop right up, but a more common word, like "for" displays the error.  Thanks again for your help, I really appreciate any help i can get with this.
stop();
this._lockroot = true;
 
String.prototype.contains = function(searchString){
	return (this.indexOf(searchString) != -1);
}
Array.prototype.contains = function(searchValue){
	var i = this.length;
	while(i--) if (this[i] == searchValue) return true;
	return false;
}
 
/*function firstToUpper (word) {
	firstLetter = word.substring(1, 0);
	restOfWord = word.substring(1);
	return (firstLetter.toUpperCase()+restOfWord);
}*/
 
SearchXML = function(nodes, query, useChildElements){
	var results = [];
	for (var i=0; i < nodes.length; i++){
		for (var j=0; j<nodes[i].childNodes.length; j++){
			currNode = nodes[i].childNodes[j];
			if (useChildElements.contains(currNode.nodeName)){
				if (currNode.firstChild.nodeValue.contains(query)){
					results.push(nodes[i]);
					break;
				}
			}
		}
	}
	return results;
}
 
ElementsToSearch = function(){
	var childElementsToSearch = [];
	if (search_fields.title_check.checked){
		childElementsToSearch.push("title");
	}
	if (search_fields.author_check.checked){
		childElementsToSearch.push("author");
	}
	if (search_fields.message_check.checked){
		childElementsToSearch.push("contentText");
	}
	return childElementsToSearch;
}
 
DisplayNodes = function(nodes, field_txt){
	field_txt.htmlText = "";
	var entry;
	var separator = "<br>_______________________<br><br>";
	for (var i=0; i<nodes.length; i++){
		entry = "";
		entry += "<b>"+ nodes[i].childNodes[0].firstChild.nodeValue +"</b><br />";
		entry += nodes[i].childNodes[1].firstChild.nodeValue +"<br />";
		entry += "<i>"+ nodes[i].childNodes[2].firstChild.nodeValue +"</i><br /><br />";
		entry += nodes[i].childNodes[3].firstChild.nodeValue;
		field_txt.htmlText += entry + separator;
		scrollCheck();
			
	}
}
 
search_highlight = new TextFormat();
search_highlight.color = 0xFF0000;
search_highlight.italic = true;
 
HighlightOccurences = function(str, field_txt, format){
	if (!str.length) return (0);
	var start = field_txt.text.indexOf(str);
	var end = start + str.length;
	while (start != -1){
		field_txt.setTextFormat(start, end, search_highlight);
		start = field_txt.text.indexOf(str, end);
		end = start + str.length;
	}
}
 
 
var posts_xml = new XML();
posts_xml.ignoreWhite = true;
posts_xml.onLoad = function(success){
	if (success){
		search_fields._visible = true;
	}else field_txt.text = "Error loading XML";
}
search_fields._visible = false;
posts_xml.load("athm.xml");
 
search_fields.title_check.title_txt.text = "Title";
search_fields.author_check.title_txt.text = "Author";
search_fields.message_check.title_txt.text = "Message";
 
search_fields.search_btn.onRelease = function(){
	if (search_fields.query_txt.text.length < 3){
		field_txt.text = "Please use a search term with 3 or more characters.";
		return (0);
	}
	var searchElements = ElementsToSearch();
	var nodesWithQuery =	SearchXML(
								posts_xml.firstChild.childNodes,
								search_fields.query_txt.text,
								searchElements
							);
	
	if (nodesWithQuery.length){
		DisplayNodes(
			nodesWithQuery,
			field_txt
		);
	}else{
		field_txt.text = "No results for "+search_fields.query_txt.text+".";
		return (0);
	}
	
	HighlightOccurences(
		search_fields.query_txt.text,
		field_txt,
		search_highlight
	);
}

Open in new window

athm.txt
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
wal_toorCommented:
Hellow,

I am currently working on this. It takes some rewriting of several functions. I'll have a working solution by tomorrow.

greetz
walter
0
 
wal_toorCommented:
Hellow again,

Whell that was tough, but it works.

I have changed the scripts so flash searches the xml file record by record. When flash finishes a record it goes to the next record. Flash will not freeze. I have tested this with a xml file twice your size, and it still operates good. I have added the cdata tags in the xml file. Flash could'nt handle the htmlentities in the xml file.

I suggest you study the files you can download here.

http://rapidshare.com/files/219557585/xml_search.zip.html

Good luck.

greetz,
walter


0
 
wal_toorCommented:
Ow.. one not though.

If you search for a frase like "for" it will take flash some time do dislplay the text in the textfield (there is a lot of text to dislplay). So do not be afraid that flash freezes.

greetz,
walter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now