?
Solved

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

Posted on 2009-04-02
7
Medium Priority
?
521 Views
Last Modified: 2013-11-11
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
Comment
Question by:dojoHobo
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:wal_toor
ID: 24056995
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
 

Author Comment

by:dojoHobo
ID: 24063022
Hey walter,
0
 

Author Comment

by:dojoHobo
ID: 24063496
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:wal_toor
ID: 24085971
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
 
LVL 8

Accepted Solution

by:
wal_toor earned 1000 total points
ID: 24113755
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
 
LVL 8

Expert Comment

by:wal_toor
ID: 24113772
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question