We have now scripts, which will do the final job to complete our search. In this example we execute the function findSubgroup_1 (subGroup), which searches for blond or black models who speak a foreign language. After looping through the haircolor childnodes we ask by an if statement if count04>=0. This makes sure we include all nodes. Counting nodes in flash starts with 0, which we have to include as well. Then we ask if there are any nodes with the attribute "foreign", since these are the ones we are looking for disregarding others. Finally we want to make sure that there are no undefined nodes.
If there was success and there were nodes then we want to know the names and descriptions, which are held by the var modelName and modelDescription. Since our description of the model can be in a textfile or in the XML file itself as the first child of a node, we have to distinguish that by another if statement. Finally we want to show a list of the matches, which is done by the function for the listbox chooseModel(modelName,modelDescription);, which is explained below. We write actionscript such as this one for all the search categories. You can see another example following this one.
//now we have to access all the childnodes, here for those which contain the attribute name "foreign"
function findSubgroup_1 (subGroup) {
for (var count04=0; count04 <= subGroup.childNodes.length; count04++) {
myModel04 = subGroup.childNodes[count04];
//"count04" will count all childnodes starting from 0.
//only the nodes with a positive match will be listed.
if(count04>=0 && myModel04.attributes.language == "foreign" && myModel04 != undefined){
modelName = myModel04.attributes.name;//name attribute
//the var modelDescription holds the nodeValue of the firstChild of each node
modelDescription=myModel04.firstChild.nodeValue;
//but when there is no nodeValue and therefore firstChild is undefined, then
//this means we have an attribute "ID" and modelDescription will hold the
//attribute ID, which is a text document.
if (modelDescription == undefined){
modelDescription=myModel04.attributes.ID;
}//here we now execute the function for the listbox with the
//two arguments "modelName", which holds the name of the models
//and "modelDescription", which holds a nodeValue or the ID attribute.
chooseModel(modelName,modelDescription);
}
}
}
//this is essentially the same as in the previous chapter, except we will list
//all childnodes of a particular parent node.
function findSubgroup_2 (subGroup) {
for (var count04=0; count04 <= subGroup.childNodes.length; count04++) {
myModel04 = subGroup.childNodes[count04];
if(count04>=0 && myModel04 != undefined){
modelName = myModel04.attributes.name;
modelDescription=myModel04.firstChild.nodeValue;
chooseModel(modelName,modelDescription);
}
}
}