Apr 05 2008

Parsing Google Calendar XML in Flash CS3 AS3

Published by admin at 6:49 pm under Classes & Packages, Flash AS3, Projects

Honestly, when I first attempted to parse and access Google Calendar XML from flash I had a lot of trouble with it. I spent a lot of hours being frustrated by XML namespaces (it was the first time I’d had to use them) and consequently had to move to the forums at actionscript.org. I was helped by a guy with the alias “wvxvw” (which is a nice palendrome, lol) so I’d like to extend credit to him for giving me a nudge in the right direction. Anyway, lets get on to the code.

First things first you need a URLLoader object to load Google Calendar’s xml. This looks like:

var xmlReq:URLRequest = new URLRequest(xmlURL);
var xmlLoader:URLLoader = new URLLoader(xmlReq);

xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
xmlLoader.addEventListener(Event.COMPLETE, xmlGCALLoaded);

To pass the XML’s URL to the URLLoader you need to use the URLRequest class, but its really simple because the constructor takes your URL as an argument so its only one line of code. In fact, if you want it even shorter you could do this:

var xmlLoader:URLLoader = new URLLoader(new URLRequest(xmlURL));

Either way is fine, obviously. Its important to note here that I am using the “full” version of the code that Google Calendar passes to you. If you use the basic you will find it much more difficult to work with the event startTime and endTime. After that be sure to add the proper Event Listeners. The IOError will be thrown if the URL doesn’t return any data (and perhaps for some other reasons too that I don’t know about). The Security Error is thrown when you violate your sandbox (which is a complicated idea, you should look it up if you’d like more info). And of course, the Event.COMPLETE is what is triggered when the data is finished loading, which takes us to the next block of code:

private function xmlGCALLoaded(evt:Event):void {
var xmlData:XML = XML(evt.target.data);
   var ns:Namespace = xmlData.namespace();
}

In this function we take the supplied data (our XML) which is noted as “evt.target.data” and put it into the xmlData variable. Using the method supplied by the XML class we can access the first namespace available to us and place it into the “ns” variable. A namespace in XML is denoted by a tag attribute “xmlns” for instance in google’s opening tag:

This will be useful when we try to access different parts of the XML google has given us. From this first namespace we call any of the properties about the calendar it’s self such as the author or id:

var author:String = xmlData.ns::author;

However, I imagine the primary reason you want this XML data is to access the “entry”, better known as the calendar events. To do this you’ll want to make another namespace (there is a deeper namespace level for accessing events and event properties). In AS3 we find out how many “entry” tags there are, loop through them, and pull the info that we want from it and place them into array or objects or whatever we’d like:

var numEntries:int = xmlData.ns::entry.length();
var xmlObj:Object;

for (var j:int=0; j<numEntries; j++) {
   var gdns:Namespace = xmlData.ns::entry[j].namespace("gd");
   xmlObj = new Object();
   xmlObj.eventTitle = xmlData.ns::entry[j].children().(name().localName == "title");
   xmlObj.startDate = createXMLDate( xmlData.ns::entry[j].gdns::when.@startTime );
   xmlObj.endDate = createXMLDate( xmlData.ns::entry[j].gdns::when.@endTime );
   xmlObj.eventWhere = xmlData.ns::entry[j].gdns::where.@valueString;
   entries.push(xmlObj);
}

We get the number of entries from the first namespace’s entry.length() which is a function provided by the XML Class. Then we loop those entries and for each one find the new namespace (also, this namespace was provided for us in the first tag, I just chose to do it this way instead). With this new namespace we are able to access the inner node of the given entry. The use of the “@” symbol lets us access the attributes of the tags, which is where the data we are seeking is. Anyway, once you have looped all the entries they are available to you in an easy to use array format.

If you’d like to see the full working AS3 Class for the Google XML Parser, you can download it from this link: googleCalendarXML Class

The rar’d version (includes the .fla for compilation) can be found from this link:gCalXML Project

To see a full project that implements this Google Calendar XML Parser, click here.

Popularity: 68% [?]

7 responses so far

7 Responses to “Parsing Google Calendar XML in Flash CS3 AS3”

  1. [...] eWealth.com Internet Marketing Forum – Affiliate Forums wrote an interesting post today on Parsing Google Calendar XML in Flash CS3 AS3Here’s a quick excerptParsing Google Calendar XML in Flash CS3 AS3 Published by admin under Flash AS3 Classes/Packages, Flash AS3 Tutorials, Web Design Honestly, when I [...]

  2. Ben Slaytonon 07 Apr 2008 at 6:27 pm

    WOW cool, can you post your fla???

    So i can see an better example?

  3. adminon 07 Apr 2008 at 9:18 pm

    Hey Ben,
    I posted the class and and .fla so you can check it out. Thanks for taking a look.
    Enjoy,
    Kyle

  4. Cedricon 06 May 2008 at 1:38 pm

    Hey,

    I thought this was supposed to embaded the Google calendar into a flash website. I tried your .fla and it’s blank. I see some data coming up in the OUTPUT window but the flash movie is empty.

  5. adminon 06 May 2008 at 2:16 pm

    Hi Cedric,
    This particular code doesn’t embed the Google Calendar in your flash movie. This is just an example of how to parse Google Calendar’s XML. However, I HAVE written code that inserts a calendar into a flash video and can run off of Google Calendar :) Here is the URL:
    http://www.kylebrekke.com/wordpress/2008/a-dynamic-flash-as3-calendar-that-parses-basic-xml-and-google-calendar-events-in-atom-xml-and-ical-format/

    Feel free to use that project for a simple insert (it also has lots of customization variables set at the top of the class for easy changing) or use the example on how to parse Google Calendar’s code to make your own.

    Let me know if you have any other questions.

    Thanks,
    Kyle

  6. Bobon 03 Jun 2008 at 11:17 am

    Hi Kyle,

    Thanks for the excellent AS3 resources. I have a question about the gCalXML Project that you provided above. Can you explain how you would attach the “entryObj.where” and “entryObj.title” information contained in the googleCalendarXML Class to a list box on the main time line?

    Thanks,
    Bob

  7. adminon 03 Jun 2008 at 2:46 pm

    Sure Bob,
    All event information is saved inside the “CalendarEvent” object. If you have multiple events (which you likely do) then an array is made of these “CalendarEvent” objects, thus as you iterate through the array you can access the individual information of each object (calendar event).

    This “CalendarEvent” object is used by the xmlFeed class to store what the xml feed gets from Google. You can find it as a property of the xmlFeed class, shown in the Calendar.as file this way:

    private var eventFeed:xmlFeed; //instantiates the xmlFeed

    eventFeed.events; //all events as an array called “events” as a property of eventFeed

    To expose this information to the stage you have a number of options. If you’d just like to make the array available, then change the private instantiation of eventFeed to public, and these events are available as .eventFeed.events. Otherwise, you can use getters/setters to expose it with more control. If you need help with those, refer to my tutorial: http://www.kylebrekke.com/wordpress/2008/write-your-first-class-ever-in-flash-as3-with-access-to-the-stage-and-even-dispatching/

    Hope that helped, let me know if you need me to clarify anything.

    Thanks,
    Kyle

Trackback URI |