2 Replies Latest reply on May 4, 2009 6:04 PM by ntsiii

    How to Find the Index of Element in the XMLListCollection

    alice_data Level 1

      Hi,

       

      I have a burning question here, and I wonder if anyone has tried to do this.
      I have an XML file that is read into the xml collection, and for each of the names that appear in the file (pre-declared somewhere else) , I tried to remove the element.

       

        However, it either gives me -1, if I do trace(slc.getItemIndex(xml.@name).toString()); when they exist, or tell me #3594 as shown in the error below:

       

      Here is the code:

       

      private function checkBox_change(event:Event):void{    
                 xlc = new XMLListCollection(searchResult..book);  
                     len = searchResult..publisher.length();              
                     for (idx=0;idx<len; idx++) {
                         if (checkBox[idx].selected == false) {                          
                                 for each (name in input_name2) {  
                                 for each (xml in xlc){
                                 if ((xml.@name == checkBox[idx].label) && (xml.@pub == name)){    
          trace(xml.getItemIndex(xml.@name).toString());
          //I get an error that says 3594: getItemIndex is not a recognized method of the dynamic class XML.                            
                    
                                 }
                             }               
                         }   
                }  
           }
            }

       

      Could anyone please give me some pointers on how to remove the desired element? I do know how to use splice.

      Thanks for your help.

       

      Alice

        • 1. Re: How to Find the Index of Element in the XMLListCollection
          Michael Borbor Level 4

          Alice you could do something like this

           

          delete myXML.(@ISBN=="4689997865")

           

          Sincerely,

           

          Michael

           

          El 04/05/2009, a las 11:54, alice_data <forums@adobe.com> escribió:

           

          >

          Hi,

          >

          I have a burning question here, and I wonder if anyone has tried to 

          do this.

          I have an XML file that is read into the xml collection, and for 

          each of the names that appear in the file (pre-declared somewhere 

          else) , I tried to remove the element.

          >

            However, it either gives me -1, if I do 

          trace(slc.getItemIndex(xml.@name).toString()); when they exist, or 

          tell me #3594 as shown in the error below:

          >

          Here is the code:

          >

          private function checkBox_change(event:Event):void{

                     xlc = new XMLListCollection(searchResult..book);

                         len = searchResult..publisher.length();

                         for (idx=0;idx<len; idx++) {

                             if (checkBox[idx].selected == false) {

                                     for each (name in input_name2) {

                                     for each (xml in xlc){

                                     if ((xml.@name == checkBox[idx].label) && 

          (xml.@pub == name)){

              trace(xml.getItemIndex(xml.@name).toString());

              //I get an error that says 3594: getItemIndex is not a 

          recognized method of the dynamic class XML.

          >

                                     }

                                 }

                             }

                    }

               }

                }

          >

          Could anyone please give me some pointers on how to remove the 

          desired element? I do know how to use splice.

          Thanks for your help.

          >

          Alice

          >

          • 2. Re: How to Find the Index of Element in the XMLListCollection
            ntsiii Level 3

            getItemIndex() requires you pass in a reference to an item in the collection, not a string.

             

            And it is a method of XMLListCollection (and arrayCollection), not a method of XML.

             

            Like Michael says, using an e4x expression directly with delete is the best way to go.  Note to use delete, you must use a direct expression.  You can't put the node you want to delete in a variable and then delete that.