Skip to content

Adventures in xml

2006 January 26
tags: , , , , , , ,
by Lichen Rancourt

I live in a coding netherworld. Given instructions, I can write code. Given code, I can usually happily change it to meet my needs. I understand most languages, intellectually, but I do not speak them. So when I want to do something, know it can be done but don’t know how to do it, I am further dogged by rarely being able to effectively communicate the need to either a search or a person who I know has the answer. Over time this has produced quite a lot of anxiety and frustration. I am, nevertheless, intrepid and tend to barge ahead – ignorance is so often bliss. The most recent of these forays involves xml and rss.


We have a library news page, which is completely hand coded. I wanted to generate an rss feed for the content, but then repurpose the feed to also generate the news page archive. Since the content is not database generated, I only want to enter it once. My understanding of xml is that it renders content portable and this should be easy.

I started with the feed itself. With a quick search and some instructions it was up and running in no time. I was jublilant. I then turned my attention toward importing the xml content into my page. “Xml makes content portable,” I thought to myself, “this should be a snap.” All I need to do is associate the html page with the xml page, map the tags together, and off I go. It is not that easy. CSS didn’t do it there was too much else included on the pages. I need to get the xml into the html (layout), not apply layout (css) to xml. XSLT seemed too much for such a simple (although gaining complications by the minute) project. Do I write a DTD? That seemed ill advised. I found some promising stuff about Data Islands and data binding seems perfectly logical… but I was horrified to learn that it only works with Internet Explorer. Exasperated, deflated, and complemplating resignation, I turned to my interpreter, who delivered a shattering blow, “huh, I don’t know if you can do that.” Being the helpful chap he is, Casey sent me a couple javascript solutions: a Script.aculo.us (you can learn some AJAX, he encouraged… I’d had enough learning for one day) and Feed to javascript. I’m ashamed to say that his kind help was met only with my righteous indignation – this should be easy!

Defeated, I went to the gym to deflate. I returned the next day to an email from Casey (aka, my hero) with a tidy little php function designed to parse out the xml and deliver me the pieces to do with what I want. I changed it a bit to fine tune and, must admit, that I think it’s brilliant.

function rssrenderer($feed, $count=50) {

// fetch the RSS feed
$xmlraw = utf8_encode(file_get_contents($feed));

// parse it
$xml = simplexml_load_string($xmlraw);

// read through each item and render it
$i = 1;
foreach ($xml->channel->item as $item) {

if($i > $count) break;
$i++;

$title = ($item->title);
$link = ($item->link);
$date = ($item->pubDate);

echo "<a href=\"$link\">$title <br /><i>Posted on $date</i>";
}
}

I then called it up in the right moment on my display page and, viola, worked like a charm. Easily reused, too, for other feeds. Here’s the xml-generated page.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS