Update: I have updated and fixed some minor functionalities of this script and also changed its name to XML Generator DIR to differentiate from the new flavor XML Generator DB. Although the usage still the same you should check the new post instead.

So here it is! The 2nd version of the XML generator that will make a lot easier to create XML configuration files. As the previous version this one is focused in the creation of configuration files for image galleries, music players and related stuff where you need to list all files in a directory. The difference is that in this version you can create almost any XML structure in a simpler way.

While writing the 1st-version post I had this idea to use a model file instead of tweaking PHP variables and arrays. Different from before now you have freedom to define your XML file. You can have how many nested tags you want and also you can have attributes.

Writing this script was fun. I used PHP and the DOM library for the job. I won’t be explaining the code in details here because it already has a lot of comments but I may write a post explaining the DOM in more details based on readers feedback.

The XML model

For this script to work you need to create a model according to your needs. In this model you use placeholders to customize the output. Take a look at this sample:

<?xml version="1.0" ?>
<images date="{callback:today}">
	<image id="{id}" name="{pathname}">
  		<caption>{input:caption}</caption>
  		<format>{callback:discoverOrientation}</format>
  		<size>{callback:filesize}</size>
	</image>
</images>

This XML is a template of a one used for creating a configuration file of images. Look that we have the root tag images and its child tag image. The XML Generator 2.0 will detect it and will duplicate the child node for every file found in the directory. It will also create an XML file for each supplied directory. The placeholders {…} will be replaced by their meanings.

Placeholders

This script supports six different placeholders that you can use in your model. Here is a list of all of them and their explanation:

  • {filename} - If used in any child node it’ll be replaced by the name of each file listed in the directory. If used in the root node it’ll be replaced by the name of the XML file.
  • {dirname} - It’ll be replaced by the name of the current directory being read.
  • {pathname} - Again, if used in any child node it’ll be replaced by the pathname of each file listed in the directory. If used in the root node it’ll be replaced by the pathname of the XML file.
  • {id} - It’s only available for child nodes and it’ll be replaced by an auto-increment number.
  • {input:info} - This placeholder causes the script to ask for user input when running through CLI. The info value should be any informative text that you want to display when asked for data input. If run through other interfaces, the script will replace it by an empty space instead.
  • {callback:function} - This placeholder will call the informed function passing the pathname as an argument. It’ll be replaced by the function returned value.

File Configurations

The last configuration step is to define some variables directly inside the script. Open the xmlgen2.php and edit from line 29 up to line 38. Here is what you will see:

28
29
30
31
32
33
34
35
36
37
38
// list of directories to be read ...
$directoryList = array('images');
 
// name of the XML file
$xmlFileName = 'images.xml';
 
// name of the XML model
$xmlModelName = 'model.xml';
 
// if the script should invert Windows slashes ('' to '/')
$invertSlash = true;

Running the Generator

Now the fun part! You can call the script from two different ways. You can call it through the webserver from the browser as you would normally do but you can also execute it through the command line interface (CLI).

Calling the script through the webserver gives you the option to pass a list of directories separated by commas to the script via the GET variable dirlist. These directories will be appended to the ones previously defined directly in the source code.

	http://localhost/images/xmlgen2.php?dirlist=myphotos,landscape

If you execute the script through the CLI you gain the possibility of using the {input:} placeholder and interact with the processing. Also, you can pass the directories as the script arguments.

	$ php xmlgen2.php myphotos landscape

Note: To run the above command on Windows you may need to add the php executable to your system path. To do that type: set path = %path%;c:\directory\of\php\; in your command prompt then restart it.

If you have any doubt or suggestion leave a comment that I will be glad to help.

Files:

XML Generator 2.0
model.xml