2 Replies Latest reply on Nov 8, 2006 10:05 PM by rkschulz

    advanced XML filtering

    rkschulz Level 1
      Hi,

      In short: How do I filter an XMLList in ActionScript rather than E4X, i.e. I am looking for a
      replacement of myXmlList.(@attrib == someValue) with myXmlList.filter(someFilterFunction)
      which has the same behaviour (i.e. the result does not contain copies).

      The long story: I have an application that operates on an XML structure. It uses the E4X filter
      mechanism to generate a number of filtered views to display, using

      filteredList:XMLList = myXmlList.(@attrib == someValue)

      I now hit the situation that the filter functionality is more complicated (I basically need access to the whole object and perform a few calculations to decide whether it's in the list). First quesiton: Is there an easy way to do this straight on the XMLList?

      I tried something like this

      var filteredList:XMLListCollection = new XMLListCollection(new XMLList());
      for each (var n:XML in myXmlList) {
      if (logic to decide whether n is in here) {
      filteredList.addItem(n); (*** see below)
      }
      }
      filteredList = XMLList(filteredList);

      But this seems add each item again to myXmlList (so they are all in there twice now) - no what I want.
      Second question: What exactly is the addItem call doing? Why is it also adding to the XMLList n is from?
      I tried

      filteredList.addItem(n.copy());

      which does not add the items to myXmlList but it makes a copy which is not what I want (I am updating the
      XML and don't want to have copies around). This whole approach may be complete nosense, I am just
      starting to get my head around the XML processing.

      Any help or pointers in the right direction are much appreciated.
      Thanks,

      Robert
        • 1. Re: advanced XML filtering
          peterent Level 2
          You are so close. You are saying the right words but not conjuring the right spell. You do want a filtering function - you want a filterFunction:

          var bigList:XMLListCollection = new XMLListCollection( originaldata );
          bigList.filterFunction = uniqueFilter;
          private function uniqueFilter( item:Object ) : Boolean {
          var test:XML = XML(item);
          if( /* some test here */ ) return true; else return false;
          }
          bigList.refresh();

          If you look at the documentation for XMLListCollection and then examine the inherited properties and methods, you'll see the filterFunction property and the refresh method.

          I just wrote an article on my blog about filtering collections. Although I used ArrayCollection, it should similarly for XMLListCollection.

          Filtering Collections - weblogs.macromedia.com/pent

          Basically the filterFunction is given an element of the Collection and must return true if the element should appear in the collection's view or false if should not. This does not eliminate the item from the Collection, just from the view of the Collection. This way you can change the criteria and refresh() the collection.

          If you have a DataGrid's dataProvider bound to the Collection you'll see it just when the refresh() is done.
          • 2. Re: advanced XML filtering
            rkschulz Level 1
            Thanks Peter!

            I was actually a lot further away from then it sounded like, but I am
            where I want to be now ;-)

            Bascially, I used the things coming out of the E4X expressions as
            DataProviders directly (not wrapped in XMLListCollections).

            I am still not quite sure I fully understand how this works, but doesn't matter:
            I re-wrote everything using the filter approach, got rid of all the E4X filter expression (wrapped them all in XMListCollections and wrote filter functions) and now it all works as it should.

            Thanks again for your help!

            Robert