Skip to main content
Inspiring
September 29, 2006
Question

Making a "search" box...

  • September 29, 2006
  • 41 replies
  • 2375 views
Okay, let's say I have an XML file like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<pages>
<page url="01.swf" search="one" />
<page url="02.swf" search="two,2" />
<page url="03.swf" search="three,3,tres" />
</pages>


Then, in Flash, I have loaded the XML file into a "navXML" variable, thereby "two,2" would be the value of "navXML.firstChild.childNodes[1].attributes.search", for example...

Making sense so far? - good!...

I want to make a search box. Basically a text entry box (say, "search_txt") that when a MC is pressed (say, "search_mc"), it will take the text in the search box and check to see if that text falls into any of the "search" attributes of that XML file....

I know this would involve a "for" loop, and I'm sure I'd have to first somehow parse the data in the "search" attributes by a "," (since some of the "search" attributes have more than one value), and maybe put them into an Array or something?...

I dunno - I know that this is a lot of questions at once, but I just wanted to give out my whole scenario so that hopefully someone could give me some idea of how to get started. Like, if my thinking is correct (above), then I suppose my first question would be how to get the "search" data paresed out by the "," and into Arrays?...

(or, maybe I'm WAY off base, and someone has made a better version of this that they can share!)

Thanks!...
This topic has been closed for replies.

41 replies

Inspiring
September 30, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Participating Frequently
September 30, 2006
Yea, the return object is a little strange, sorry about that part. But basically this is how I search for data on a smaller scale. Definitly not somthing with tens of thousands of entries.

I also have a general problem with turning the XML into another object. Why not just use the XML and XMLNode properties not to mention the XPathAPI to do what they were designed for. But I think with AS3 this become a dead issue since its will be supporting XML as a native datatype.

Anyway, sorry about the oops with the return value.

Tim

(By the way what was with all the post? :-)



Inspiring
September 30, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 30, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)



Inspiring
September 29, 2006

"TimSymons" <webforumsuser@macromedia.com> wrote in message
news:efjpj9$fhk$1@forums.macromedia.com...
>I did not test this but you could start with something like this and
>probably
> optimize it for speed.
>
>
>
> Tim
>
>
>
>
> // search_txt, search_mc already exist on timeline
> //
> // assume also that the following XML data was read/parsed into the navXML
> variable:
> /*
> <?xml version="1.0" encoding="iso-8859-1"?>
> <pages>
> <page url="01.swf" search="one" />
> <page url="02.swf" search="two,2" />
> <page url="03.swf" search="three,3,tres" />
> </pages>
> */
>
> search_mc.onRelease = searchXML;
>
> function searchXML():XMLNode {
> var searchStr:String = search_txt.text.toLowerCase();
> var i:Number;
> var j:Number;
> var nodeIndex:Number;
>
> for (i=0; i<navXML.firstChild.childNodes.length; i++) {
> var searchArr:Array = new Array();
> searchArr =
> String(navXML.firstChild.childNodes .attributes.search).split(",");
> for (j=0; j<searchArr.length; j++) {
> if (searchStr == searchArr.toLowerCase()) {
> nodeIndex = i;
> break;
> }
> }
> if (nodeIndex != undefined) {
> break;
> }
> }
>
> return navXML.firstChild.childNodes[nodeIndex];
> }
>


Couple things here. I am not sure what you are returning to? search_mc is
a MovieClip, so you are returning a Node to the onRelease? Think that may
be a typo.

As far as speed... couple things. I am not sure about actionscript and its
lookup times for executions like this... but, first things first.

You actually don't need to break up the string into other words. Thanks to
the indexOf method of the String Class. You can just do a test
if (yoursearchValue.indexOf(searchWord) > -1) {
found it
}

I also think you may be better served parsing your XML into a native Flash
Datatype like an Array of Object like this:
myFinisedObject = [{url:"01.swf", keywords:"one"}, {url:"02.swf",
keywords:"two,2"}]

To build that Array you could do the loop like listed in the previous reply.
Then when you do your search you would just loop this array of objects
checking for the indexOf on the keywords property.

I also think it depends on how much data you are going to be handling here.
The parsing into the myFinisedObject gets better the more stuff you give it
I think (again I am not sure how fast it is in comparison - but if you try
it, let me know)