7 Replies Latest reply on Nov 5, 2007 10:52 AM by Newsgroup_User

    E4X Search Query

    Level 7
      With the new E4X XML, can you do searches on attributes like a SQL search?

      for example: if I was tagging a database, it might look like this: "SELECT
      title FROM songs WHERE title LIKE '%blue%'" and it would return all records
      that had "blue" in the title..

      is there something you can do like that with XML in AS3 using the new syntax?
      musicXML.songs(@title == "blue") or something...

      or would I have to loop through all the XML records?


        • 1. Re: E4X Search Query
          Level 7
          The String.search method might help you out:


          var musicXML:XML =
          <song title="Callin' the blues" />
          <song title="Black Day in July" />
          <song title="Blue Monday" />

          var searchResults = musicXML.songs.songs(@title.search("blue")>-1);


          • 2. Re: E4X Search Query
            Level 7
            That should be:

            var searchResults = musicXML.songs.song(@title.search("blue")>-1);

            • 3. Re: E4X Search Query
              DJ_James Level 1
              ok thats cool, would it work like this if I wanted to search on artist, or title, or genre or something?

              <song title="Callin' the blues" artist="some guy" genre="blues"/>
              <song title="Black Day in July" artist="that guy" genre="disco" />
              <song title="Blue Monday" artist = "some that" genre="disco"/>
              • 4. E4X Search Query
                DJ_James Level 1
                I'm trying to do this in the new AIR beta for Flash CS3 first off :)

                but I'm doing this:

                var musicXML:XML =
                <song title="Blue Heaven" artist="The Guy" disc="2000 Vol 1" track="1" genre="Pop" />
                <song title="Blue" artist="Funk Guy" disc="2000 Vol 1" track="2" genre="Pop" />
                <song title="Heavenly Blue" artist="The Funk" disc="2000 Vol 1" track="3" genre="Pop" />
                <song title="Blueish" artist="The Funk Guy" disc="2000 Vol 1" track="4" genre="Pop" />
                <song title="Blue Funk" artist="Blue Funk" disc="2000 Vol 1" track="5" genre="Pop" />
                <song title="Guys Funky Blues" artist="Blues Guy" disc="2000 Vol 1" track="5" genre="Pop" />

                /*var XMLLoader:URLLoader = new URLLoader();
                XMLLoader.load(new URLRequest("musicDB.xml"));

                function xmlCompleted(event:Event):void {
                musicXML = new XML(event.target.data);

                searchType.addItem({label:"Song Title"});

                searchBtn.addEventListener(MouseEvent.CLICK, searchShit);

                function searchShit(event:Event):void {
                var searchResults:XML;

                if(searchType.selectedItem.text == "Song Title") {
                searchResults = new XML(musicXML.music.song(@title.search(txtField.text)>-1));
                } else if(searchType.selectedItem.text = "Artist") {
                searchResults = new XML(musicXML.music.song(@artist.search(txtField.text)>-1));
                } else {
                searchResults = new XML(musicXML.music.song(@genre.search(txtField.text)>-1));


                and for whatever reason, it would give me the errors:
                1120: Access of undefined property title
                1120: Access of undefined property artist
                1120: Access of undefined property genre

                so I change the "if then" statement to have a "." after the song and before the "(" like this:

                if(searchType.selectedItem.text == "Song Title") {
                searchResults = new XML(musicXML.music.song.(@title.search(txtField.text)>-1));
                } else if(searchType.selectedItem.text = "Artist") {
                searchResults = new XML(musicXML.music.song.(@artist.search(txtField.text)>-1));
                } else {
                searchResults = new XML(musicXML.music.song.(@genre.search(txtField.text)>-1));

                but then it gives me security errors:

                Attemping to launch and connect to Player using URL X:/Air Tests/musicSearchTest/Untitled-1-app.xml
                [SWF] Untitled-1.swf - 80573 bytes after decompression
                *** Security Sandbox Violation ***
                SecurityDomain 'app-resource:/Untitled-1.swf' tried to access incompatible context ' http://adobe.com/apollo'

                is this something in AIR because I've used the "xml.item@attribute == value)" when just creating a swf before and it worked (not the search like this, but..)

                • 5. Re: E4X Search Query
                  Level 7
                  Sure. But I imagine you could compare genre and artist without searching the
                  strings -- would depend on how much control you have over the attribute
                  values -- i.e. Disco, DISCO, etc...

                  var discoResults = musicXML.songs.song(@genre.toString()=="disco");

                  ... or you could use a regular expressions

                  // case insensitive search genre="disco", genre="dISCo"
                  var regExDiscoResults = musicXML.songs.song(@genre.search(/^disco/i)>-1);

                  var guyArtistResults = musicXML.songs.song(@artist.search(/guy/i)>-1);
                  var someArtistResults = musicXML.songs.song(@artist.search(/some/i)>-1);

                  • 6. Re: E4X Search Query
                    Level 7
                    Haven't had time to look at Apollo...

                    But two things to note:

                    1. The path to the <song> nodes is musicXML.song, not musicXML.music.song --
                    the <music> node is the root.
                    var musicXML:XML =
                    <song title="Blue Heaven" />
                    <song title="Blue" />
                    2. searchResults should be an XMLList

                    var searchResults:XMLList;
                    searchResults = musicXML.music.song(@title.search(txtField.text)>-1);

                    • 7. Re: E4X Search Query
                      Level 7
                      Oops. I DID forget the dot(.)

                      All my examples should be