Skip navigation
AS3Beginner333
Currently Being Moderated

AS3 and XML, HELP!

Mar 29, 2012 12:09 AM

Tags: #flash #as3 #xml #actionscript3

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

 
Replies
  • kglad
    72,212 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 29, 2012 7:02 AM   in reply to AS3Beginner333

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 7:07 AM   in reply to AS3Beginner333

    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);

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 12:51 PM   in reply to AS3Beginner333

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 6:14 PM   in reply to AS3Beginner333

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 29, 2012 6:29 PM   in reply to AS3Beginner333

    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 .

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points