AS3 Wordpress Blog
Posted by Elliot.
It isn’t new but I finally had a chance to install Wordpress and hook my Flash blog system to the front end.
The main differences is the rss2 feed from Wordpress compared to the feed from blogger.
The rss2 feed from Wordpress
The Formatting of the RSS2 adheres to the web standards which is great. In order to deal with the XML feed you need to understand Namespaces to get the correct xml child and general XML usage.
This is part of the feed that I get from Wordpress:
example xml
There is a lot of useful information here and may seem a bit of a jumble. But it doesn’t matter, what I was after was the usual child tags of information contained with the item tags. In this case I want the publishing date, category, heading and the body text. These are the names of the tags I needed:
title – the title of the post.
pubDate – the date of publishing.
category – The category associated with this post.
content:encoded - this is the body copy of the post, as you’ll noticed the tag name has a namespace identity of “content:encoded”.
This script shows the loading the XML feed and then breaking down the child nodes items into these child tags that we need. These are then placed into an inline argument of an object. This Object is then added to an array. The reason I use an array for the various objects related to the section is that an Array is a position relative variable, meaning that I can use the order to arrange the sections. This is very useful as I finally used the category names to order the array (I’ll show you later).
package com.elliotrock
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
import flash.system.Security;
/**
* RSSParser includes methods for
* converting RSS XML data into HTML text.
*/
public class RSSParser extends EventDispatcher {
// this is the url location of the feed
public var url:String = "http://blog.elliotrock.com/?feed=rss2";
public var rssXML:XML;
public var rssTitle:String;
private var myLoader:URLLoader;
private var dataError:Event;
public var setup:Array;
//
public function RSSParser() {
/*
You need to consider your security settings at this point and make sure you have a crossdomain.xml
policy file at your root of your server, if you are trying to load from another domain then you need to
consider the Security.loadPolicyFile(url); method.
*/
var rssXMLURL:URLRequest = new URLRequest(url);
myLoader = new URLLoader(rssXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
public function xmlLoaded(evtObj:Event):void {
rssXML = XML(myLoader.data);
/*
* new_setup is contained within this function
*/
var new_setup:Array = new Array();
if (rssXML.namespace("") != undefined) {
default xml namespace = rssXML.namespace("");
}
for each (var item:XML in rssXML..item) {
/*
* this pulls in the various tags within the rssfeed
*/
var itemTitle:String = item.title.toString();
var itemLink:String = item.link.toString();
var itemPubDate:String = item.pubDate.toString();
var itemCategory: Object = item.category.toString()
/*
*/
var itemDescription:String = item.description.toString();
/*
I need to search through the repetitions of the namespace "content" used in the XML to the
one associated to
*/
var contentNS:Namespace = new Namespace(item.namespace("content"));
default xml namespace = contentNS;
//
// the .. is sort of like asking for this association
var itemContent:String = (item..encoded).toString();
/*
add these items as inline arguments to an object and add to the end of the new_setup array
*/
new_setup.push({title_:itemTitle, description_: itemDescription, content_: itemContent, pubDate_: itemPubDate, category_: itemCategory});
}
setup = new_setup;
}
}
The public var setup is the array that contains all of this useful information. How I grabbed the content:encoded tag was using this namespace method:
var contentNS:Namespace = new Namespace(item.namespace("content"));
default xml namespace = contentNS;
var itemContent:String = (item..encoded).toString();
This is from Colin Moock’s book Essential Actionscript page(?). It is a nice and re-usable piece of code that first pulls out all the variations of content ( go back and have a look at the raw XML RSS2 feed above) and then looks for the first usage of “content” and “encoded”. Namespaces is a weird concept to get use to, I suggest doing a it of research or to get Essential Actionscript3 by Colin Moock.
To get the information out of this array use the short cut access methods and get the variable associated with what you want. Say I want the title of the first item then I use:
trace(setup[0].title_);
// Flash happenings
Just remember that arrays are zero based so they start from zero. I hope that helped.