as3使用多个元素搜索xml

as3使用多个元素搜索xml,xml,actionscript-3,search,xmllist,Xml,Actionscript 3,Search,Xmllist,我试图在as3中搜索这个XML文档 <mineral> <name>Calcite</name> <color>White</color <diaphaneity>Opaque</diaphaneity> </mineral> <mineral> <name>Spangolite</name> <color>Blue&l

我试图在as3中搜索这个XML文档

<mineral>
    <name>Calcite</name>
    <color>White</color
    <diaphaneity>Opaque</diaphaneity>
</mineral>
<mineral>
    <name>Spangolite</name>
    <color>Blue</color>
    <color>Green</color>
    <color>Blue Green</color>
    <color>Dark Green</color>
    <color>Emerald Green</color>
    <diaphaneity>Transparent</diaphaneity>
    <diaphaneity>Translucent</diaphaneity>
</mineral>
<mineral>
    <name>Barite</name>
    <color>Yellow</color>
    <color>Honey</color>
    <diaphaneity>Transparent</diaphaneity>
    <diaphaneity>Translucent</diaphaneity>
    <diaphaneity>Opaque</diaphaneity>
</mineral>
<mineral>
    <name>Landauite</name>
    <color>White</color>
    <diaphaneity>Transparent</diaphaneity>
    <diaphaneity>Translucent</diaphaneity>
</mineral>
<mineral>
    <name>Sapphire</name>
    <color>Blue</color>
    <color>Blue green</color>
    <diaphaneity>Transparent</diaphaneity>
    <diaphaneity>Translucent</diaphaneity>
</mineral>
使用“trace(mineralList)”命令,它将成功地跟踪整个XML文件,如果我将其更改为“trace(xmlData.mineral.(color==“White”);”,那么它将跟踪一个值为“White”的元素所包含的所有节点


方解石
白色
不透明的
蓝柱石
白色
透明的
半透明的
然而,如果我搜索蓝色而不是白色,它不会找到任何东西。我猜这是因为包含值为“蓝色”的元素的矿物节点也有多个其他颜色值。这就是我试图评估的问题


我需要能够搜索颜色并拉出所有具有其中一个颜色值的节点,而不管是否具有其他颜色值。

在代码中,如果颜色==白色,则节点只有一个节点。在其他情况下,它有多个节点。所以试着这样做

 private function ParseMinerals(mineralXML:XML):void
    {
       var  mineralList:XMLList  =  mineralXML.mineral;
       for each (var mineral:XML in mineralList)
       {
        if(mineral.color.length()>1)
            {
           for each(var color in mineral.color)
           {
               if(color == "Green")
               {
              trace(mineral);       
               }    
            }
        }
        else
        {
           if(mineral.color == "White")
           {
            trace(mineral);     
           }
        }
   }

实际上,我通过以下代码实现了这一点:

首先,我为定义结果时要包含的结果创建了一个数组,并创建了一个变量来记录循环所处的结果编号

var resultArray:Array = new Array;
var resultNum:Number = 0;
然后,我用XML数据创建了一个共享对象

function ParseMinerals(mineralXML:XML):void
{
     //create a var called memory and datatype it to SharedObject and name the
     //file "attributes"
     var memory:SharedObject = SharedObject.getLocal("attributes");

     //create an XMLList containing the information in mineralXML.mineral
     var  mineralList:XMLList  =  mineralXML.mineral;

     //save the data in mineralList to shared object
     memory.data.mineralList = mineralList;
     memory.flush();
}
我没有在将xml加载到ParseMinerals函数中后运行代码,而是将其放在一个名为“search”的新函数中,该函数在按下“search”按钮时运行

如果我搜索“蓝色”,程序就会跟踪

Result #1: Calcite 
Calcite
Result #1: Spangolite
Result #2: Sapphire
Spangolite, Sapphire

我希望这能帮助任何试图实现类似目标的人。

我尝试将您的代码建议应用到我的代码中,结果得到一个1084:语法错误:在冒号之前输入
function search(event:MouseEvent):void
{
    //load shared file
    var memory:SharedObject = SharedObject.getLocal("attributes");

    //create a variable that is the length of the list of minerals
    var len:int = memory.data.mineralList.length();

    //create variables to temporarily store information regarding minerals color 
    //and name
    var thisColor:String;
    var thisName:String;

    //create var that increments for each time you loop through a "mineral" node
    for (var i:int = 0; i < len; i++) {
        //create var that increments for each time you loop through a "color"
        //element  within a single "mineral" node
        for (var c:int = 0; c < xmlData.mineral[i].color.length(); c++) {
            //make thisColor equal to the current color that the for loop is on
            thisColor = xmlData.mineral[i].color[c];
            //make thisName equal to the current name that the for loop is on
            thisName = xmlData.mineral[i].name;
            //if the color that the for loop is currently on is equal to the
            //color you are searching for...
            if (thisColor == memory.data.mineralColor){
                //... then put the name of that mineral into an array
                resultArray.push(thisName);
                //... add 1 to the current result number
                resultNum ++;
                //... and trace the current result number and the name of the
                //... mineral corresponding to the color you are searching for
                trace("Result #" + resultNum + ": " + (thisName));
            }
        }
    }
    //reset array
    resultArray.length = 0;

    //reset result number
    resultNum = 0;
}
Result #1: Calcite 
Calcite
Result #1: Spangolite
Result #2: Sapphire
Spangolite, Sapphire