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.
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.
This script supports six different placeholders that you can use in your model. Here is a list of all of them and their explanation:
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; |
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.
6 Responses
Carlos Coelho
May 11th, 2007 at 2:18 pm
1Thank you vega for this new version, tested this file with success, it will help a lot
Bmud
March 26th, 2008 at 5:43 pm
2I’m very new to PHP and your try{} functions give me php errors. But as I’m realizing that this code is not meant to be executed by my end users, I think I’ll have to look elsewhere.
Thanks for whatever help your well-commented code might bring me.
fromvega
March 27th, 2008 at 12:00 am
3Hello Bmud,
Those “try” aren’t functions but PHP5 constructs. So it means you should have PHP5 to run the script. It seams to me that you have PHP4.
Also, you should look at the newer version of this script at http://fromvega.com/wordpress/2007/05/13/xml-generator-dir/
Bye!
peter
May 11th, 2008 at 1:06 pm
4hello!
i have tested it but over shell i get
Parse error: parse error in line 120
what das that mean?
regards
peter
fromvega
May 16th, 2008 at 3:38 pm
5Hi Peter, I don’t know for sure. Are you sure you are using PHP5? If not maybe this is the case. If yes maybe you made a mistake while editing the file? Give it a check, and also try the newer version of this script at http://fromvega.com/wordpress/2007/05/13/xml-generator-dir/
Tomás
June 21st, 2008 at 9:24 am
6Man you saved my life. I was about to program something like this for myself, you saved me a lot of wasted time!
Thanks,
RSS feed for comments on this post · TrackBack URI
Leave a reply