Home Articles Uncategorized Refreshing & Formatting XML

Refreshing & Formatting XML

In a recent client project, it called for videos with play lists. I used Longtail’s JW player to handle this. The setup required XML files for the play list. Having not worked that much with XML before, I ran into two problems which I eventually found a solution for.

Problem 1: Making XML refresh

Long story short: browsers cache XML. So they won’t refresh XML upon updates. The simple solution I found was to add ?123 to end of the file name. Here’s a normal link to a XML file in my HTML:

<a href="http://yoursite.com/videos/playlist.xml">link</a>

All you gotta do is add the following to the end of the URL:

<a href="http://yoursite.com/videos/playlist.xml?123">link</a>

It can be whatever numbers you want, or however long. Just keep the question mark and the following numbers (without spaces) and that will force the browser to get the latest version of the XML file every time someone reloads the page.

Don’t ask me why browsers cache XML locally, but I had to find a solution. Otherwise, visitors would be viewing old content, and I would have to tell them to clear their cache – that’s unacceptable!

Problem 2: Formatting XML with HTML

Another problem I ran into was the inability to format the output with HTML. The video play list required line breaks and bold subheadings for its descriptions. I tried to wrap the pieces of content in HTML tags, but no formatting was showing up. Instead, the actual HTML markup showed up. This is because XML parses everything between the tags.

The solution was simple, XML required a bit of extra code to tell it to render the HTML formatting and to not parse. Whenever I used a <b> element like this:

<annotation>The description required <b>bold text</b></annotation>

I needed to add these additional codes before and after the HTML elements like so:

<annotation>The description required <![CDATA[<b>bold text</b>]]></annotation>

Therefore, a <br/> element would be turned into this: <![CDATA[<br/>]]>. CDATA stands for (Unparsed) Character Data. This allowed my HTML to render. More about XML and CDATA here.

While the only times I use XML are for music and video play lists, I’m glad to know how to control its output and solve that annoying caching problem.