8 Replies Latest reply on Oct 24, 2008 4:46 PM by ntsiii

    Searching on XML

    MixMastaJames Level 1
      I want to know if there is a way to "search" on an xml file and bind that data to a dataGrid based on that search..

      basically like if you were searching through music:

      <song id="0">
      <title>My Song</title>
      <artist>The Artist</artist>
      </song>
      <song id="1">
      <title>Another Song</title>
      <artist>The Artist</artist>
      </song>
      <song id="2">
      <title>Disco Fever</title>
      <artist>The Artist</artist>
      </song>
      <song id="3">
      <title>Funky Guy</title>
      <artist>My Artist</artist>
      </song>

      and then search for "song" in the song field or "The Artist" or "Artist" and have it return anything that has those parameters in them. And if thats possible, is it possible to search on more than one.. say all artists with "Artist" in the field and "Song" in the title
        • 1. Re: Searching on XML
          m_hartnett Level 3
          Put the XML into an XMLListCollection and then use the filterFunction to filter out the data you want to see. Take a look at the api for the XMLListCollection / filterFunction.

          here is a link that explains
          http://blog.flexexamples.com/2007/08/22/sorting-and-filtering-data-in-an-xmllistcollection /#more-85
          • 2. Re: Searching on XML
            MixMastaJames Level 1
            Thanks I'll check it out!
            • 3. Re: Searching on XML
              Level 7

              "New to AS3" <webforumsuser@macromedia.com> wrote in message
              news:gdpv94$rpf$1@forums.macromedia.com...
              >I want to know if there is a way to "search" on an xml file and bind that
              >data
              > to a dataGrid based on that search..
              >
              > basically like if you were searching through music:
              >
              > <song id="0">
              > <title>My Song</title>
              > <artist>The Artist</artist>
              > </song>
              > <song id="1">
              > <title>Another Song</title>
              > <artist>The Artist</artist>
              > </song>
              > <song id="2">
              > <title>Disco Fever</title>
              > <artist>The Artist</artist>
              > </song>
              > <song id="3">
              > <title>Funky Guy</title>
              > <artist>My Artist</artist>
              > </song>
              >
              > and then search for "song" in the song field or "The Artist" or "Artist"
              > and
              > have it return anything that has those parameters in them. And if thats
              > possible, is it possible to search on more than one.. say all artists with
              > "Artist" in the field and "Song" in the title
              >

              http://dispatchevent.org/roger/as3-e4x-rundown/


              • 4. Re: Searching on XML
                MixMastaJames Level 1
                Haven't looked at the XMLListCollection yet.. but did check out the dispatchevent link. I did get it to work with the .(node ==) method... but here is the REALLY fun part :)

                can anyone tell me how to do a "partial search" on a value? like in SQL:
                SELECT * FROM mycol WHERE myfield LIKE '%this%'

                can you do that with those methods? I can't seem to find anything that will allow it.
                • 5. Re: Searching on XML
                  MixMastaJames Level 1
                  nevermind.. got it :)
                  musicSearchXML = music_XML.songs.song.(title.search(textSearchField.text)>-1);
                  • 6. Re: Searching on XML
                    ntsiii Level 3
                    Also,
                    If you need complex calculations within an e4x expression, you can call out to a function of your own. The () just requires a boolean value, so you can do something like below, where I wanted to a bit of string manipulation within the expression:

                    xlFilteredItems = _xmlData..item.(itemContains(attribute("item"),sFilterString))

                    and the function:
                    private function itemContains(sItem:String, sMatch:String):Boolean
                    {
                    sItem = sItem.toLowerCase();
                    sMatch = sMatch.toLowerCase();
                    return (sItem.indexOf(sMatch) != -1);
                    }//contains

                    Tracy
                    • 7. Re: Searching on XML
                      MixMastaJames Level 1
                      Tracy,

                      Would this be able to work on muliple fields.. say if I wanted to search on an artist AND song title? logically it doesn't seem like it would work...
                      • 8. Re: Searching on XML
                        ntsiii Level 3
                        Sure, that would work. Consider an expression like this:
                        var xl:XMLList = xml..(doMySelectionLogic(*));

                        I am not exactly sure that "*" is the right syntax to pass in the current node, it might be ".", but whatever, we could figure that out.

                        In doMySelectionLogic(xmlCurrentContextNode:XML):Boolean, we could use an e4x expression to find any value anywhere in the xmlhierarchy we needed. We could use parent().parent().. to move directly up the hierarchy. We could use ordinary expressions to move down. We can loop over any subset we want. We could examine global variables or any other in-scope value we wanted. We could use Math.whatever. Anything.

                        Tracy