Using XML For The First Time
15th April 2003 · Last updated: 5th October 2016
I did it! I finally thought of some uses for XML on our work website, so I thought 'What the heck - I'll give it a go'. I converted a page of meeting dates that span several screens deep. The page still looks identical, but it's now fed from a database I set up using Microsoft's free XML Notepad. That is really quite a handy tool once you figure it out.
Before, I was scrolling up and down an HTML file looking for bits of data to change. Now the file is much smaller, and any changes can be made very simply via the database file instead.
XML code is very, very easy to write. It's just like HTML but with a free reign over what tag names you want to use. The idea is to describe the data between them. No more learning what HTML tags like
<th> are supposed to mean.
For instance, my new file uses tags like
<time> and so on. The hard part is converting the data so it displays on a web page. Luckily if you use PHP, it's a doddle. I downloaded a couple of useful scripts that proved useful in the past for displaying free news headlines.
There are two basic ways to parse XML and convert it to HTML. Actually, if you use IE6 or Mozilla, you don't even need to do that - the browser will show the file as a document tree, much like it appears in Notepad. IE6 has a built-in parser you can use, but it'll only work in that browser. Whereas Mozilla can add style to an XML file and present it as intended on screen. But the real way is to parse it is with PHP and then decide what you do next. You might decide not to output it as HTML at all, but WML code for mobile phones.
The main two parsing methods you can use are firstly to convert the XML tags into meaningful HTML ones. Let's take some sample XML code I just made up:
<river>Hudson</river>. As it stands, because there is no
<river> tag in HTML, the browser will simply ignore it. But we might convert it to HTML code that works, retaining the data inbetween the tags, so it now looks like
<div id="river">Hudson</div> or
<table><tr><td>Hudson</td></tr></table>. PHP can do this using arrays and this is the method I used at work, as it's nice and simple. The drawback is that your XML must be fairly basic and similar in structure to how the HTML page will be.
The second approach is to feed the XML file through the PHP parser and output only the lines you want. This way you can filter the file first. You might have a list of ten names and addresses, but you only want the first one. Or the first three. You might also want to show just the names and not the addresses. All of this and more is possible.
Using PHP ensures a wide range of browsers are able to view the results too. All the user sees on the page is the final HTML code you generate - they don't see any XML at all.
Then you might offer a choice of outputs for the user. They can rearrange the data on screen as they wish, simply by offering a link that reparses the same XML file. Or even loads another one instead. The permutations are endless, offering a hint towards a much more dynamic web. Not only that, but XML files can be easily understood and converted into other formats. Many sites offer part of their content as a free XML file you can link to and turn it into content on your own site.
I've some more pages at work I want to try converting to run off an XML database. For now though, the feeling of glory having got one page working is enough to bask in. Besides, XML is one of those cool names that it's good to add to your list of skills, even if you barely know how to use it. And with time, like PHP and HTML, my knowledge of it is sure to grow. I feel it's just too useful to ignore.