Solved

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

Posted on 2009-04-02
7
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to how to record live broadcast.

738 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