# Xml/text to array to be searched

**URL:** <https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253>\
**Category:** Uncategorized\
**Created:** [July 31, 2003, 3:27pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253 "2003-07-31T15:27:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbrewer](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@jbrewer](https://forum.kirupa.com/u/jbrewer)\
**Post date:** [July 31, 2003, 3:27pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/1 "2003-07-31T15:27:07Z")

</div>

okay, here is the problem… i have a text file and i have it in xml format as well, in it is a list of 55 companies that i want to load into my movie and have them stored as an array that i can search. I am having a hard time making it happen, though… i am new to using xml and the format is just simply:

```php
<companies>
	<name> Akai<name/> 
	<name> Altec Lansing <name/> 
	<name> Audio Centron <name/> 
	<name> Ampeg <name/> 
	<name> Allen and Heath <name/> 
	<name> Biamp Systems <name/> 
	<name> Cerwin Vega <name/>
	<name> Community <name/> 
	<name> Crest Audio <name/> 
	<name> Crate <name/> 
	<name> DBX <name/> 
	<name> Digitech <name/> 
	<name> DOD <name/> 
	<name> Dynacord <name/> 
	<name> Electrovoice <name/> 
	<name> Eden Electronics <name/> 
	<name> EAW <name/> 
	<name> Fatar <name/> 
	<name> Fender <name/> 
</companies>

```

somthing like that… and the text file is just the variable companies=“blah, blah and blah”  
any help would be tremendously appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 31, 2003, 4:33pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/2 "2003-07-31T16:33:47Z")

</div>

this is how the xml file should look like

```php
<?xml version="1.0"?>
<companies>
    <name>Akai</name> 
    <name>Altec Lansing</name> 
    <name>Audio Centron</name> 
    <name>Ampeg</name> 
    <name>Allen and Heath</name> 
    <name>Biamp Systems</name> 
    <name>Cerwin Vega</name>
    <name>Community</name> 
    <name>Crest Audio</name> 
    <name>Crate</name> 
    <name>DBX</name> 
    <name>Digitech</name> 
    <name>DOD</name> 
    <name>Dynacord</name> 
    <name>Electrovoice</name> 
    <name>Eden Electronics</name> 
    <name>EAW</name> 
    <name>Fatar</name> 
    <name>Fender</name> 
</companies>

```

and this AS parses it into an array called ‘companies’ 🙂

```auto
myxml = new XML()
myxml.ignoreWhite = 1
myxml.onLoad = function() {
	this = this.firstChild.childNodes
	Companies = []
	for(i=0; i<this.length; i++) Companies* = this*.firstChild
	trace(Companies)
}
myxml.load("companies.xml")

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 31, 2003, 6:53pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/3 "2003-07-31T18:53:58Z")

</div>

the only problem is that the trace only outputs the first company “akai”… i am looking at how to fix, though.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 31, 2003, 7:01pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/4 "2003-07-31T19:01:53Z")

</div>

if you just have Companies\* = this\* you get the xml output to a string of \<name\> blahdeeblah \</name\>… so now how do you split it up?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 31, 2003, 8:42pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/5 "2003-07-31T20:42:17Z")

</div>

i don’t get you. What I posted worked fine. Don’t use this\*, you should use this\*.firstChild (and that’s how i did it in my script… )

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 31, 2003, 8:59pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/6 "2003-07-31T20:59:13Z")

</div>

your original code only gave an output of the first company - akai. How do you get it to output all the “children” of \<companies\>???

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 2:37pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/7 "2003-08-01T14:37:28Z")

</div>

actually, brewer… his code is correct and trace shows all the companies listed. Are you trying to incorporate this into a textfield?

btw… if you’re not getting all of the children…hehe… check your xml file… make sure you changed all of your “/” to the correct location.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 4:19pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/8 "2003-08-01T16:19:07Z")

</div>

you were correct… shame on me for not double checking my xml. I could not figure out why it wasn’t displaying… duh.

Yes i am eventually trying to display it in a text field. The story is i have an input field that people can enter in a companies name to see if this company does warranty repair work on that companies gear. I wanted to avoid having to search a database and so i thought i could bring in all the names via and xml doc, turn it into an array and search thru that. I actually got all the way to the array, but searching is turning up the index of the array, not the content. If you have any suggestions, please let me know.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 5:13pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/9 "2003-08-01T17:13:16Z")

</div>

lol, i posted the correct xml file for you, but you used the older one… 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 5:21pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/10 "2003-08-01T17:21:50Z")

</div>

```auto

arrayToBeSearched = ["Kirupa", "lostinbeta", "senocular", "ahmed"];
function searchArray(theArray, searchstring){
	var outputArray = [];
	for(var i = 0; i<theArray.length;i++){
		if(theArray*.toLowerCase().indexOf(searchString.toLowerCase()) != "-1"){
			outputArray.push(theArray*);
		}
	}
	return outputArray;
}
//returns another array of matched elements.
trace(searchArray(arrayToBeSearched, "LOS")) //outputs 'lostinbeta'

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 5:33pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/11 "2003-08-01T17:33:46Z")

</div>

that’s nice, why not make it a prototype? 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 5:36pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/12 "2003-08-01T17:36:10Z")

</div>

Yeah, I probably will… that was just a 5min example though to help him get the idea really though.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 5:38pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/13 "2003-08-01T17:38:02Z")

</div>

Done(-:

```auto

Array.prototype.searchArray = function(searchString){
	var outputArray = [];
	for(var i = 0; i<this.length;i++){
		if(this*.toLowerCase().indexOf(searchString.toLowerCase()) != "-1"){
			outputArray.push(this*);
		}
	}
	return outputArray;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 6:57pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/14 "2003-08-01T18:57:01Z")

</div>

so here’s what i am trying and my brain must be fried cos i am going nowhere…

search\_btn.onPress = function() {  
Companies.searchArray(input);  
trace(input);  
if(input == outputArray){  
display = outputArray;  
}  
}  
input being the variable from the input\_txt…  
gonna keep at it, though.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 7:51pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/15 "2003-08-01T19:51:01Z")

</div>

The function I wrote returns an array containing the results of the search, so for the code you wrote, you would need to do something like this:

```auto

search_btn.onPress = function() {
var searchResults = Companies.searchArray(input);
trace(input);
display = searchResults;
}

```

You can use the as tags for code formatting as well.

[_as] code goes here(without asterisks)[_/as]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 8:20pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/16 "2003-08-01T20:20:12Z")

</div>

okay, that makes perfect sense, but when i tried it, display displayed all of the companies, not just the one i entered into the search…  
this is almost funny…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 1, 2003, 10:00pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/17 "2003-08-01T22:00:11Z")

</div>

Do you have any more code, or could you post your .fla?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 7:10am UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/18 "2003-08-02T07:10:04Z")

</div>

okay, here it is…

```auto
myxml = new XML();
myxml.ignoreWhite = true;
myxml.onLoad = function() {
	this = this.firstChild.childNodes;
	Companies = [];
	for (i=0; i<this.length; i++) {
		Companies* = this*.firstChild;
	}
	//trace(Companies);
};
myxml.load("warranty.xml");

//search
Array.prototype.searchArray = function(searchString) {
	var outputArray = [];
	for (var i = 0; i<this.length; i++) {
		if (this*.toLowerCase().indexOf(searchString.toLowerCase()) != " -1 ") {
			outputArray.push(this*);
		}
	}
	return outputArray;
};

search_btn.onPress = function() {
	input = "";
	var searchResults = Companies.searchArray(input);
	trace(input);
	display = searchResults;
};

```

that is it and the xml doc is the same as the one before…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 9:49pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/19 "2003-08-11T21:49:01Z")

</div>

is there anyone out there that can help with why this code is spitting out all the companies in the array instead of just one???

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 12, 2003, 12:23pm UTC](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253/20 "2003-08-12T12:23:02Z")

</div>

in the code above, in my array search prototype, the -1 has two spaces around it. Remove them, or take it out of a string.

```auto

Array.prototype.searchArray = function(searchString) {
        var outputArray = [];
        for (var i = 0; i<this.length; i++) {
                if (this*.toLowerCase().indexOf(searchString.toLowerCase()) != -1) {
                        outputArray.push(this*);
                }
        }
        return outputArray;
};

```

[Next page](https://forum.kirupa.com/t/xml-text-to-array-to-be-searched/27253.md?page=2)
