8 Replies Latest reply: Mar 29, 2012 6:29 PM by sinious RSS

    AS3 and XML, HELP!

    AS3Beginner333

      Hi

      I have no idea where to post this, so pls forgive me if im in the wrong place, but I have an Assessment due tomorrow, I'm only 4 weeks knew to AS3 and Flash, and confused as, so pls forgive me if i also look silly asking this question.

       

      The issue is loading data from an XML file and having it present in a Flash website. Its a book review thing (i've altered the XML and AS3 for posting here so please bare that in mind), so the XML looks a little like this

       

      <books>

          <book>

              <bookName>The Monsters</bookName>

              <genres>

                  <genre>Thriller</genre>

                  <genre>Crime</genre>

                  <genre>Comedy</genre>

              </genres>

              <description>about the mummies.</description>

              <image>mummies.jpg</image>

              <reviews>

                  <review>

                      <date>16/07/2011</date>

                      <name>unnamed</name>

                      <info>

                          <rating>5</rating>

                          <why>blah blah</why>

                          <theGood>it was good...</theGood>

                          <the Bad>it was bad…</theBad>

                      </info>

                  </review>

              </reviews>

          </book>

      <books>

       

      but each Book has multiple reviews, i've just shown you one. Anyway, i need to present this information in 3 dynamic text fields when a book is selected in my ComboBox… after a lot of trouble i got the basics of it. So in the Genre box the genres will display, and in the Description box the description will display, and in the Review box i can get all the information, but only with all the tags. I need it to display without any tags, it would be wonderful if i could figure out how i could put date, name etc in front of that information as well, but I cant even begin to figure that one out.  For the life of me i cannot figure it out. below is the code I have so far:

       

      //Load XML

      var booksXML:XML = new XML();

      var loader:URLLoader = new URLLoader();

      var request:URLRequest = new URLRequest("gigGuide.xml");

      loader.addEventListener(Event.COMPLETE,loaderOnComplete);

      loader.load(request);

       

      function loaderOnComplete(event:Event):void

      {

         booksXML = new XML(event.target.data);

         var books:Array = new Array({label:"Select a Book"});

         for each (var book:XML in booksXML.band)

            books.push({label:book.bookName.toString(), data:book.description.toXMLString(),

                       infoLocal:book.genres.genre.toString(), infoDate:book.reviews.review.toString(),

                       infoImage:book.image});

       

         bandCB.dataProvider = new DataProvider(bands);

      }

       

      //ComboBox changeable

      bookCB.addEventListener(Event.CHANGE, changeHandler2);

      function changeHandler2(event:Event):void

      {

         genre_txt.text = ComboBox(event.target).selectedItem.infoLocal;

         description_txt.text = ComboBox(event.target).selectedItem.data;

         reviews_txt.text = ComboBox(event.target).selectedItem.infoDate;

         empty_mc.tex = ComboBox(event.target).selectedItem.infoImage; //doesn't work

      }

       

      any help would be greatly appreciated, and the image doesn't work, i've already faced facts that im not going to get that one to work

      Thank you in advance

        • 1. Re: AS3 and XML, HELP!
          kglad CommunityMVP

          your first error is your xml is malformed.  assuming <the Bad>it was bad...</theBad> is a forum artifact and not really in your xml, you only need to close your books tag.  otherwise, fix both.

          • 2. Re: AS3 and XML, HELP!
            dmeN Community Member

            From what I can see you have a few problems. In your loaderOnComplete you set the data provider with 'bands' but create a variable called 'books'.

            Also, you want to be using the XMLList object to parse your data out. It's much simpler.

             

            Have a look at the following:

             

            var booksXML:XML;

             

            function loaderOnComplete(e:Event):void

            {

                      booksXML = new XML(e.target.data);

                      var books:XMLList = booksXML.book;

                      trace(books[0].bookName);

             

                      var bookReviews:XMLList = books[0].reviews.review;

                      trace(bookReviews[0].name);

            trace(bookReviews[0].date);

            }

             

            First we get all the book xml objects in an XML List - the trace traces book number 0's name - The Monsters.

             

            Next, you can get all the reviews for a book in another XML List. Here we use the first book - book[0] and trace out the review's name and date.

             

            You can iterate through an XMLList as it has a length() method - (not a length property)

             

            Also, loading an image for a book would be easy - you just grab the image property of the current book and then use a Loader to load it.

             

            trace(books[0].image);

            • 3. Re: AS3 and XML, HELP!
              AS3Beginner333 Community Member

              Thank you both very much for your quick replies, and sorry about the spelling errors, that has occurred when I've retyped for posting.

               

              The actual code that I have above does works, it just doesn't display the way I need it to. for starters there is multiple reviews to each book, and I need all reviews to display in the review field, and the code above does this, however all the tags are there. I need the tags to be gone, my teacher said I need "a for each loop within each one and push the data the same way", and after googling for days I've had no luck figuring this out, and he hasn't replied for further help (I study online, and can't hunt down teachers for help). This is probably a silly question, but I couldn't find another way to figure it out.

               

              Again, thank you both very much for your replies, your help is greatly appreciated

              • 4. Re: AS3 and XML, HELP!
                sinious CommunityMVP

                The real world is littered with this. If we just give you the code to answer your question you'll just be back here every time you have a new question. The real question is, are you willing to learn Flash?

                 

                When he says you need a 'for each' loop in there, he's trying to make you realize you can do loops inside loops. So while you're looping over each book, inside the same loop you can (unlimitedly) create more loops for other things.

                 

                For example, when you:

                for each (var book:XML in booksXML.band) { .. }

                 

                He's telling you that you need ANOTHER for each () {} loop inside there (where I put the ..). A loop inside another loop is a common programming practice.

                 

                Look at the code you already wrote, duplicate it and add it inside itself in the for each () {} loop. Try to change the values of what you're setting in the loops conditions (between the parenthesis()). Try to figure out by what you already see how to extract what you need. All the code is already there, you just need to modify it a little bit.

                • 5. Re: AS3 and XML, HELP!
                  AS3Beginner333 Community Member

                  Thanks for a little bit more of a direction... I've spent the last few hours trying to follow your direction, havn't accomplished anything so far, but will keep trying.

                  • 6. Re: AS3 and XML, HELP!
                    sinious CommunityMVP

                    I'm not trying to deny you help but when you preface your request with "I'm in college trying to learn this" it tells me if I hired you I'd get a person unwilling to learn the software, hoping people in a forum could help them get a project done. You don't want to be that person. You've been tipped off that you can add loops inside loops. Just consider the data that is available inside each loop and what loops are for (iterating over rows of data). You have most of the code already written to do this, you just need to duplicate it inside of the loop you already have and use the data you're getting in the loop to provide a second loop with data to iterate over.

                     

                    I'm sorry your teacher is absent but every day of your flash career you'll be faced against obstacles like this and will have a deadline. You can't rely on a forum. I believe you can take a quick look at the XML classes you're already using along with the wise advice here (like XMLList) to extract the data you need.

                    • 7. Re: AS3 and XML, HELP!
                      AS3Beginner333 Community Member

                      Thank you... I'll do just that

                      • 8. Re: AS3 and XML, HELP!
                        sinious CommunityMVP

                        If you have a specific question about something feel free to ask. We're all here to help . But you really should try to learn enough to do your own homework .