Twitter
RSS

Parse Google Calendar XML with Actionscript 3.0 the Right Way

February 25, 2010 - 6 Comments Bookmark and Share

calIf you ever sit down to try and parse Google calendar XML data in Actionscript (specifically AS3), you’ll find that there is a lot of misinformation on the web, including many poor, broken examples of code. I’d like to try and demystify some of the problems behind parsing Google calendar XML by showing how it is done the right way.

How to Find the URL to Some Google Calendar XML

Google does a great job of giving you direct access to the XML of any calendar that may appear on your personal calender. You even have the option of making your private calendars public, thereby getting access to your own calendar XML feed. In Google calendar, if you click Settings (Img.1), then click the first orange XML button (Img. 2), Google gives you a URL to your calendar’s XML. You must make the calendar you want to use in your AS3 project public.

Click Settings to find the Google Calendar XML URL

Click Settings to find the Google Calendar XML URL for your AS3 project

Click the Orange XML Button

Click the Orange XML Button

Once you have the URL to your XML, you may want to append some sorting options to your XML.

Sort Your XML Before You Use It

By default, Google adds events to your XML by the date the event was created on the calendar, not by the event’s start date or end date. This is odd but there is a quick fix. By appending a little more data to your calendar URL, you have have sorted XML. I recommend adding this to the end of your URL:

?orderby=starttime&sortorder=ascending&max-results=5

Now that you have sorted, usable XML, let’s add it to your Actionscript.

The Code

Here is a short example of how to parse your Google Calender XML in Actionscript 3.0.

var xmlLoader:URLLoader = new URLLoader();
var xml_data:XML = new XML(); 
xmlLoader.addEventListener(Event.COMPLETE, load_xml); 
 
xmlLoader.load(new URLRequest("http://www.google.com/calendar/feeds/0p18vi9o7tve7uokobk4irlhb4@group.calendar.google.com/public/full?orderby=starttime&max-results=5&singleevents=true&sortorder=a"));
 
function load_xml(e:Event):void {
 
	xml_data = new XML(e.target.data);
	parse_xml(xml_data);
 
}
 
function parse_xml(xml_data:XML):void {
 
	// Number of XML / Calendar Entries
	var numEntries:int = xml_data.*::entry.length();
 
	// Loop for Event Title and Summary
	for(var i:int=0; i<numEntries; i++) {
 
		var event_text = xml_data.*::entry[i].*::title.text().toXMLString();
		var eventStartDate = xml_data.*::entry[i].*::summary.text().toXMLString();
		trace(event_text);
		trace(eventStartDate);
 
	}
 
}

If you have some basic knowledge of AS3 loaders, then this will not look very complicated. First, we create a new AS3 loader, which will grab our XML and load it up so we can put it to use. Once you actually deploy your flash site, you may need to use a PHP XML proxy file to spoof your sever into thinking the Google Calendar XML is local.

After we create the loader, we add an event listener that sends the data to our parse function after the data loads. Then, we’re off and running. Make sure you replace the Google URL with our own calendar URL (I have a demo in there now). You can also change which XML node data you’d like to get by the word “title” in the event_text variable with the name of another node in the XML. See below:

xml_data.*::NODE_YOU_WANT[ARRAY_INDEX_NUMBER].*:: NODE_YOU_WANT.text().toXMLString();

Comments

  1. Stephen(March 21, 2010)

    Hi thanks it worked fine while i was making it but as soon as i try running it on the webhost it wont work any tips?

  2. Zac(March 21, 2010)

    Hi Stephen,

    This is probably because your web host won’t allow you to load remote XML as a security feature. To get around this, try making a PHP XML proxy file. Then, instead of pointing your AS3 file to the XML directly, link the local PHP proxy file. Let me know if you need an example.

  3. Stephen(March 22, 2010)

    Yes please never done a php proxy file

  4. Zac(March 22, 2010)

    Something like this works for me. Make sure you add in the link to your remote XML.

    <?php
    // Set your return content type
    header('Content-type: application/xml');

    // Website url to open
    $url = "http://www.linktoyourXML.com";

    // Get that website's content
    $handle = fopen($url, "r");

    // If there is something, read and return
    if ($handle) {
    while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    echo $buffer;
    }
    fclose($handle);
    }
    ?>

  5. Stephen(March 22, 2010)

    Thanks that works great

  6. Zac(March 22, 2010)

    No problem!

Leave a Reply