Although this version may be an interesting study, you should check the new version instead. XML Generator 2.0 is easier and more powerful than the version presented in this post. Check it by clicking here.

This morning a friend of mine came to me asking for a script to automatically generate an XML config file. He needed a script to read directories of images and build an XML file for each one to use along his Flash gallery. It would save him a lot of time so I decided to write one specially for him. Then I realized that creating this type of XML file is a common task that many of us need to do at least once in a lifetime. Who never needed to create an XML for an image gallery or for a music player?

With this assumption in mind I decided to write a little more generic script. However, it’s still limited to the purpose of reading directories of one level only and for tags without attributes. In this post I’ll explain how you can use the XML generator script. I’ll not explain the code in details because I believe the source code is pretty well documented but if you have any doubt about it, please feel free to leave a message. I’ll be glad to help.

Before continuing let’s see an example file generated by this script, this way you can decide if it fits your needs:

<?xml version="1.0" ?>
<images>
  <image>
    <name>images/cars/ferrari.jpg</name>
    <caption>A red and expensive Ferrari</caption>
    <orientation>horizontal</orientation>
    <filesize>520676</filesize>
  </image>
</images>

The script can be executed through the webserver or through the command line. But by executing it through the command line you get some special features that I’ll talk about ahead. First you need to open the xmlgen.php file and customize it. Here are the variables you need to modify:

Inform if the script should invert Windows slashes (” to ‘/’). It’s true if you run the script on Windows and then need to move it to Linux or another Unix platform.

25
$invertSlash = true;

The list of directories the script should read and create an XML file for. Inform the path relative to the location of the xmlgen.php

28
$directoryList = array('cars', 'people', 'landscape');

The name of the XML file to be generated.

31
$xmlFileName = 'images.xml';

The name of the root tag.

34
$rootTagName = 'images';

The name of the root immediate child tags (the directory entries tag).

37
$childTagName  = 'image';

The tag list of each directory entry. You need to provide a pair of ‘tagName’ => ‘tagValue’ for each desired tag. You have the option to use placeholders for dynamism. In the source code you have the list of all available placeholders. Here I’ll discuss two of them.

57
58
59
60
$childTagList = array('name' 	=> 'images/{pathname}',
		'caption' 	=> '{input}',
		'orientation'	=> '{callback:discoverOrientation}',
		'filesize' 	=> '{callback:filesize}');

The {input} placeholder. It’s a special placeholder that just works when the script is executed through the command line. It causes the script to ask the user (you) to type the value of the tag. It’s useful when you want to insert photo captions, for instance.

The {callback:} placeholder. It’s very useful option that let you customize the value of your tags by calling your functions. It passes the pathname as the argument and expects the value of the tag in return. Some image functions like discoverOrientation are provided by the script.

Now that you have customized the script, you need to call it. As previously said you have two options: you can call the script through the webserver as usual or you can execute the script through the Command Line Interface (CLI). Here I’ll teach you how to do the later. Both ways will generate a XML file inside each folder.

Calling the XML generator through the CLI

When called through the command line the XML generator script accepts a list of directories as argument. The directories will be added to the ones pre-configured in the script.

The procedure of calling the script through the command line is very simple. You need to type the path of the PHP/CLI executable followed by the path of the script followed by its parameters. If you add the path of the PHP/CLI executable to the system path you can simply type “php” from anywhere (but you already knew that don’t you?). So the call should look something like this:

php xmlgen.php mypictures photos

Note: I just had an idea to improve this script while writing this post, making it simpler and easier to customize. Keep tuned!

Files:
XML Generator