Solved

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

Posted on 2009-04-02
7
503 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
7 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 250 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now