This post is basically written to target anyone working, or that need to be working, with XML-parsing in some CMS-system or XML-parsing in general. To start with, I must confess that this post will be quite nerdy and people without any intrest in PHP-, CSS- and/or Javascript coding will probably not find this very interesting.  In my project I’m running the latest version of Joomla! 1.5 (Joomla! 1.5.21 Stable, view my configuration here) installed on a Finnish web hotel server. The project I’m working on is to build a website for a local real estate company. The site should import the objects for sale from an XML-file provided by Kivi, an online program for real estate offices provided by Etuovi, or actually the company behind Etuovi – Alma Media.

After building a simple Joomla!-website and customizing a template for the site I started to work with the parsing of the XML-file. The file was quite complex (take a look at the XML-file here) and I tried various techniques, such as XMLDOM and XSLT, but without any success to parse the file correctly, or at least to parse it the way I wanted to. I ended up buying a script, Magic Parser, witch I can recommend to anyone that need to parce a lot of XML-information or complex XML-files. The script is not very expensive, and the support provided in the forum is great!

To get started, I just uploaded my script and got some help from Magic Parser with the coding, this is also a great service! And with this PHP code (get the complete file as a .txt here) I started to list the real estate objects for sale:

require(”kivixml/MagicParser.php”);
$count = 0;
function myRecordHandler($record)
{
$fields = array();
$currentName = ””;
foreach($record as $key => $value)
{
if (strpos($key,”NAME”))
{
$currentName = $value;
}
if (strpos($key,”VALUE”))
{
$fields[$currentName] .= $value.” ”;
}
}
// now we can just print the fields we want
… … ….
print<b>.$fields["town"].”, ”.$fields["quarteroftown"].”, ”.$fields["realtytype_id"].”</b>”;

This is how the page looks now!

PHP pages in Joomla!

This listes all the objects for sale at the moment, and I got it working fine, te next problem I had to solve was to implement this code into my Joomla!-site. The solution for that was easy to find in the extensions database on the joomla.org site, my decision fell to the PHP pages component by Fiji webdesign. The component is very easy to use and worked fine for me without any extra work, just install and enjoy. :)

Making the items clickable and showing detailed information about each item
After listing all the objects I wanted to make every object clickable to get more information about them, this I solved by using the method described in this forum post. By adding the item-id number to the link showing in the adress bar and adding this code to the PHP-file you are linking to: (This is how it looks and the complete file as a .txt here)

// ignore record if the ”item-id” is not passed
if ($record["ITEM-ID"] <> $_GET["ITEM-ID"]) return;
// rest of myRecordHandler function below e.g.

And to make the objekt linkable on the object listing page I replaced this codestring:

print<b>.$fields["town"].”, ”.$fields["quarteroftown"].”, ”.$fields["realtytype_id"].”</b>”;

with this: (complete file)

print<b><a href=’http://nymanlkv.fi/sv/details?ITEM-ID=”.$record["ITEM-ID"].”‘>”.$fields["town"].”, ”.$fields["quarteroftown"].”, ”.$fields["realtytype_id"].”</b></a>”;

So this is what we got so far!

Including parsed data into other scripts and coding
So I have now made a page listing all the objects, made all the objects linkable and a page that will show detailed information about the objects. Now I will describe how I customized the page showing detailed information. One big advantage that you have when parsing an XML-file with a script like this, is that you quite easy can put custom code or other script inside the parsed information, you can also print the information from the XML-file inside another script – this is what I will explain to you how I did in this case.

I wanted a good way to show the pictures, witch URL the XML-file provides, so I ended up using a simpel CSS-gallery script. But before I explain to you how to use that one we have to put a little bit of code into the PHP-file to parse the correct image-urls. To do this we need to replace the myRecordHandler function code with this:

function myRecordHandler($record)
{
if ($record["ITEM-ID"] <> $_GET["ITEM-ID"]) return;
$fields = array();
$images = array();
$currentName = ””;
foreach($record as $key => $value)
{
if (strpos($key,”NAME”))
{
$currentName = $value;
}
if (strpos($key,”VALUE”))
{
if ($currentName==”image_url”)
{
$images[] = $value;
}
else
{
$fields[$currentName] .= $value.” ”;
}
}
}
// now we can just print the fields we want

And to print the images we just type in this code:

foreach($images as $image)
{
print ”<a href=’”.$image.”‘ />”;
print ”<img src=’”.$image.”‘ height=’120′ width=’107′ /></a>”;
}

The script im using to show the pictures in a nice way is the Highslide JavaScript image gallery, it is also quite easy to use and even if you have very limited coding skills it almost work with copy-paste skills. After inserting the required script code in the <head> section of your PHP-page, or alternatively  used the print function to create a <head> seaction as I did, simply replace the URL in the <img src=’URL’… tag with the codestring ”.$image.” in the Highslide code to display the images. (Take a look how it turns out here and download the complete code as a .txt here)

Parsing data based upon criteria match
The next problem that came up on me was that the real estate office wanted to categorize the objekts on the site, according to the value set in the ”itemgroup_id” property (to show items based upon criteria match). This was a little bit tricky but after a while I got the help I needed from the Magic Parser forum, and in the end I just inserted this string into the PHP-file listing the objects and saved it as e new file:

// the myrecordHandler function
if ($fields["itemgroup_id"] <> ”tontit”) return; //tontit can be any value set in the
// now we can just print the fields we want

This is what I wanted to explain to you today. I wanted to share my experience with parsing XML-information because I hope that someone else can find this useful. You can se the final result of this project at nymanlkv.fi. All the relevant files can be downloaded as a zip file here, including the Highslide files but excluding the Magic Parser files that must be bought from magicparser.com.