View Full Version : xml/text to array to be searched
jbrewer
July 31st, 2003, 11:27 AM
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!
ahmed
July 31st, 2003, 12:33 PM
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' :)
myxml = new XML()
myxml.ignoreWhite = 1
myxml.onLoad = function() {
this = this.firstChild.childNodes
Companies = []
for(i=0; i<this.length; i++) Companies[i] = this[i].firstChild
trace(Companies)
}
myxml.load("companies.xml")
jbrewer
July 31st, 2003, 02:53 PM
the only problem is that the trace only outputs the first company "akai"... i am looking at how to fix, though.
jbrewer
July 31st, 2003, 03:01 PM
if you just have Companies[i] = this[i] you get the xml output to a string of <name> blahdeeblah </name>... so now how do you split it up?
ahmed
July 31st, 2003, 04:42 PM
i don't get you. What I posted worked fine. Don't use this[i], you should use this[i].firstChild (and that's how i did it in my script.. )
:)
jbrewer
July 31st, 2003, 04:59 PM
your original code only gave an output of the first company - akai. How do you get it to output all the "children" of <companies>????
Iammontoya
August 1st, 2003, 10:37 AM
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.
jbrewer
August 1st, 2003, 12:19 PM
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.
ahmed
August 1st, 2003, 01:13 PM
lol, i posted the correct xml file for you, but you used the older one.. :)
λ
August 1st, 2003, 01:21 PM
arrayToBeSearched = ["Kirupa", "lostinbeta", "senocular", "ahmed"];
function searchArray(theArray, searchstring){
var outputArray = [];
for(var i = 0; i<theArray.length;i++){
if(theArray[i].toLowerCase().indexOf(searchString.toLowerCase()) != "-1"){
outputArray.push(theArray[i]);
}
}
return outputArray;
}
//returns another array of matched elements.
trace(searchArray(arrayToBeSearched, "LOS")) //outputs 'lostinbeta'
ahmed
August 1st, 2003, 01:33 PM
that's nice, why not make it a prototype? :)
λ
August 1st, 2003, 01:36 PM
Yeah, I probably will... that was just a 5min example though to help him get the idea really though.
λ
August 1st, 2003, 01:38 PM
Done(-:
Array.prototype.searchArray = function(searchString){
var outputArray = [];
for(var i = 0; i<this.length;i++){
if(this[i].toLowerCase().indexOf(searchString.toLowerCase()) != "-1"){
outputArray.push(this[i]);
}
}
return outputArray;
}
jbrewer
August 1st, 2003, 02:57 PM
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.
λ
August 1st, 2003, 03:51 PM
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]
jbrewer
August 1st, 2003, 04:20 PM
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....
λ
August 1st, 2003, 06:00 PM
Do you have any more code, or could you post your .fla?
jbrewer
August 2nd, 2003, 03:10 AM
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[i] = this[i].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[i].toLowerCase().indexOf(searchString.toLowerCase()) != " -1 ") {
outputArray.push(this[i]);
}
}
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...
jbrewer
August 11th, 2003, 05:49 PM
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???
λ
August 12th, 2003, 08:23 AM
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[i].toLowerCase().indexOf(searchString.toLowerCase()) != -1) {
outputArray.push(this[i]);
}
}
return outputArray;
};
jbrewer
August 12th, 2003, 11:54 AM
sorry, that didn't do it either... still gives an output of all the companies. I have been trying to see if there is a way in importing the xml that we can do something different, but i am almost out of time on this and have fried my brain on it!!!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.