New Component: XMLdata

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.75 out of 5)
Loading ... Loading ...

Many websites provide their news and data feeds as a RSS / XML data feed. This new component makes it easy to add that data to your website and can also be used to make interesting Windows or Google gadgets.

Downloads

This component will work with SWiSH Max4 and MiniMax4 with build dates of 20110318 or later, and although the component is not included with the 20110318  build’s installer, it can be downloaded here. After downloading it, extract both files within the zip file to:

C:\Program Files\SWiSH Max4\Components\Utility

The exact location may be different depending on where you installed Max4, or MiniMax4. We intend including the component in future releases. On x64 systems, Max4 and MiniMax4 are by default installed into C:\Program Files (x86)\

Use

To use the component drag it from the Components Panel onto the stage.

Then define the URL, Event FunctionSearch Objects and Poll Time parameters.

When loaded, the component requests XML data from the specified URL. When the Data is received, the xml document is parsed according to the defined Search Objects. The User defined Events function is then called to process the received data.

The parameters are described below with example configurations.

Parameters

URL

This is the URL of the website that returns the xml data. If this field is left blank, no request for data is made until the SetURL() method is called (see the Methods section below).

Example URL’s:

Yahoo! Weather for Sydney Australia

http://weather.yahooapis.com/forecastrss?w=1105779&u=c

ABC News: Science and technology (ABC is ‘Australian broadcasting commission’)

http://www.abc.net.au/news/tag/science-and-technology/rss.xml

Event Function

This is the name of the user defined function in the parent object that processes the received data. The Event function copies the received data from the xml object to dynamic text objects within the movie for display.

The function has two parameters.

function Events(n,p) {
 // n is the name of the calling object
 // p is an object reference to the data area
 ...
}

This definition is similar to the Events function used by other components such as buttons etc.

The first parameter is the name of the calling object (“XMLdata”) the second parameter is an object reference to the data area. The name of the individual data items are defined by the Search Objects parameter. Each data item it is referenced as an array. This is to handle cases where the specified data occurs in multiple locations within the xml file.

For Example,  the Search Objects parameter defines a data element with the destination “mydata“. Data for the item is referenced as p.mydata[] within the events function. p.mydata[0] is the 1st element of the mydata values, p.mydata[1] is the second etc. p.mydata.length defines the total number of mydata data values.

Search Objects

This string parameter defines multiple search objects. Each new object is defined on a new line. Each line is a comma separated list defining destination,search,attribute where:

destination is the name of the location where the data is placed.

search is the fully qualified search string for the data. eg. .rss.channel.title

attribute is the optional attribute associated with the search string. The use of this value allows data to be extracted from attribute information.

An example Search Objects String that seems to work for most news feeds is shown below:

mainTitle,.rss.channel.title
heading,.rss.channel.item.title
description,.rss.channel.item.description
pubDate,.rss.channel.item.pubDate
link,.rss.channel.item.link

Within the events function, the received data would be referenced via:

p.mainTitle[], p.heading[], p.description[], p.pubDate[] and p.link[] where p is the object reference passed as the second parameter to the Events function.

Poll Time

This is the rate (in minutes) that new data is obtained from the URL. If this value is <= 0 then no additional data is obtained after the first request. It is possible to dynamically alter the poll rate and initiate a new data request via the methods: SetPoll() and SetTimer()

Trace XML

If this item is checked then a list of the received data item names as well as their status is shown in the debug panel. An example is shown below:

.rss
.rss.channel
.rss.channel.title *** Got data
.rss.channel.atom:link
.rss.channel.link
.rss.channel.description
.rss.channel.lastBuildDate
.rss.channel.language
.rss.channel.sy:updatePeriod
.rss.channel.sy:updateFrequency
.rss.channel.generator
.rss.channel.item
.rss.channel.item.title *** Got data
.rss.channel.item.link *** Got data

This example xml trace was received from a news feed. The “*** Got data” items show where a match was made with data defined in the Search Objects parameter.

Methods

These methods allow the dynamic behavior of the component to be altered by script. For example, the SetInterval() method could be used to set a different poll rate according to data received in the previous request.

SetURL(url)

This method alters the URL that is polled. After calling this function the URL is called to obtain new data.

SetPoll(t)

Updates the poll time. This updated time is used after the current poll interval expires. t is the poll time in minutes.

SetTimer(t)

Restarts the poll interval with the specified time interval. The time interval is specified in minutes.

Example 1 -Yahoo! Weather

The URL has the general format:

“http://weather.yahooapis.com/forecastrss?w=citycode&u=U

where citycode is your local City Code and  U is either f or c.

The City Code parameter is a numerical code that defines the source of the weather information. This code can be found by selecting your local city from

http://weather.yahoo.com/ and searching for your city.

The city code is then shown in the URL bar of your browser:

Once the city code is known, decide if you wish to use Fahrenheit (f) or Celsius (c) for your temperature display. You can then construct your xml data URL as:

“http://weather.yahooapis.com/forecastrss?w=citycode&u=U

where U is either f or c.

If you then visit the derived URL and view the source data (Mozilla is great for this) you will see the raw xml data the is received. I copied and pasted this data into a local text file for analysis with XmlPad. XmlPad is a free utility that makes editing and viewing xml data easy.

On expansion it shows that the received data items are:

After searching through the items, it can be seen that the temperature data is obtained from the “temp” attribute of the “rss.channel.item.yweather:condition” element.

<yweather:condition  text="Mostly Cloudy"  code="28"  temp="18"  date="Tue, 03 May 2011 12:29 pm AEST" />

Consquently the following line is added to the Search Objects parameter:

temperature,.rss.channel.item.yweather:condition,temp

This means that the attribute item temp of .rss.channel.item.yweather:condition is returned in the data item temperature. This data item is referred to as p.temperature[0] within the Events function.

Note that the search string starts with a leading “.”

In a similar way the other items of interest are added to the Search Objects parameter. the final list is shown below:

fctext,.rss.channel.item.yweather:forecast,text
fcday,.rss.channel.item.yweather:forecast,day
fcdate,.rss.channel.item.yweather:forecast,date
fclow,.rss.channel.item.yweather:forecast,low
fchigh,.rss.channel.item.yweather:forecast,high
ttl,.rss.channel.ttl
iconcode,.rss.channel.item.yweather:condition,code
lasttaken,.rss.channel.lastBuildDate
description,.rss.channel.description
temperature,.rss.channel.item.yweather:condition,temp
temperatureunits,.rss.channel.yweather:units,temperature

The events function used in the example file (xmlweather.swi) is shown below:

function Events(n,p) {
 // p.iconcode[0] contains the code for the weather icon. load that icon into the weatherIcon movie clip
 weatherIcon.loadMovie("http://l.yimg.com/a/i/us/we/52/" add p.iconcode[0] add ".gif");   
 if (Number(p.ttl[0]) > 1) {
 // set the poll rate according to the ttl value
 xmldata.SetTimer(Number(p.ttl[0]));
 }
 temp.text = p.temperature[0]; // write the current temperature
 units.text = p.temperatureunits[0]; // write the temperature units
 description.text = p.description[0]; // write the description
 updated.text = "at: " add p.lasttaken[0]; // write the updated information

 // write out the forecasts using loop as there are possibly multiple values
 var s:String = "";
 for (var j=0; j<p.fcdate.length; j++) {
 // shows as day date description min | max (one forecast per line)
 s = s add p.fcday[j] add " ";
 s = s add p.fcdate[j] add " ";
 s = s add p.fctext[j] add " ";
 s = s add p.fclow[j] add " | ";
 s = s add p.fchigh[j] add newline;
 }
 forecast.text = s;
}

Analysis

The events function copies the received data to the dynamic text objects. The received data is accessed using the second parameter, p.

The item p.ttl[0] is used to set the poll rate for the next poll according to the information obtained from the received data.

p.icon[0] contains the weather code. This is used to select the appropriate icon for the weatherIcon movie clip.

I hope you find this component and analysis useful.

Example 2 – News Feed

This example shows a typical news feed. It shows the recent topics, date of publication a brief description and a link to the full story. The Parameters used are shown below.

URL:

http://www.abc.net.au/news/tag/science-and-technology/rss.xml

Search Objects:

heading,.rss.channel.item.title
description,.rss.channel.item.description
pubDate,.rss.channel.item.pubDate
mainTitle,.rss.channel.title
link,.rss.channel.item.link

Event Function:

function Events(n,p) {
 // user to modify this function so that received data is processed
 var s:String = "";
 maintitle.text = p.mainTitle[0];

 for (var j=0; j<p.heading.length; j++) {
 if (0 == j) {
 textscroll.SetText("<b>" add p.heading[j] add "</b>" add newline);
 } else {
 textscroll.AppendText("<b>" add p.heading[j] add "</b>" add newline);
 }
 textscroll.AppendText(p.pubDate[j] add newline);
 textscroll.AppendText(p.description[j] add " ");
 textscroll.AppendText("<u><a href='" add p.link[j] add "'>Full Story...</a></u>" add newline add newline);
 }
 textscroll.SetStart(0);
}

Example Downloads

Both of the example files can be downloaded in this zip file.

Notes

Some websites do not allow the re-broadcast of their data. This may be for copyright or other reasons. In these cases you will see the information correctly when previewed in Max, but the data will not be visible when your files are uploaded to an external website.

Most rss feeds will have copyright or terms of use conditions associated with them. Please read and adhere to these copyright conditions.

Yahoo! Weather does attach specific conditions to the use of their rss feeds. See Terms of Use at http://developer.yahoo.com/weather/

 

Tags: , , , , , , , ,

One Response to “New Component: XMLdata”

  1. punto de cruz says:

    There is noticeably a bundle to learn about this. I assume you made certain good points in options also.


Switch to our mobile site