Xml/text to array to be searched

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:

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

this is how the xml file should look like

<?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’ :slight_smile:

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")

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

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?

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… )

:slight_smile:

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

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.

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.

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


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'

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

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

Done(-:


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;
}

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.

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:


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]

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…

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

okay, here it is…

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…

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

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.


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;
};