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.
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
9 Responses
Carlos Coelho
May 10th, 2007 at 1:10 am
1Hello vega thank you for more east script, for several days I researched on a xml generator in the internet without success, I guarantee that will help many developers Thank you
Oscargr
May 13th, 2007 at 2:09 am
2Another option to consider is using PEAR’s XML_Serializer and XML_Unserializer classes. They make it really, really easy to go from a PHP data structure to XML and back.
shock
May 13th, 2007 at 3:24 am
3nice work! ~~
isko mazaredo
July 7th, 2007 at 7:45 pm
4hi! your xml config generator for images is good but im thinking if you can help me with config generator for my web xml player. i just want to play all my mp3 in my website without editing my playlist.xml manualy. i just edited your xml config gen but i’m really getting hardtime coz i’m really a noob in scripting. what i want to do is just play all my mp3 in a certain folder. my old xml code is just like this…
Title Of The Song
http://www.myweb.com/mp3/1.mp3
…
…
Note: http://>
Thanks!
fromvega
July 8th, 2007 at 12:03 pm
5Hello Isko,
First I’d like to thank you for the audience. Second I recommend you to check the newer version of this script, XML Generator DIR. It makes a lot easier to generate XML files.
You can get it at the following post:
http://fromvega.com/wordpress/2007/05/13/xml-generator-dir/
Give it a try and if you again find any trouble configuring it please post another comment there that I’ll be glad to help you.
Bye!
mike
October 21st, 2008 at 12:23 pm
6thanx for the script! I have managed to adapt it to suit my requirements. I now have one problem. Thre is a Thumbs.db that is being put in one of my xml elements yet the folder does not contain this. How do i get rid of the Thumbs.db using this script
fromvega
October 27th, 2008 at 3:57 pm
7Hello mike,
to do that you would need to check for the filename. You could write something like this:
Put After: if($file->isFile()){
if($file->getFilename() == ‘Thumbs.db’) continue;
Hope it helps.
vaughan
November 2nd, 2008 at 1:13 pm
8Hi
Good work
I’m trying to create a php that reads all album/houses/kitchens , /houses/bathrooms etc and outputs xml.php.
I am using gallery2 and would like to drop this php into any folder and read it into a flash.swf whenever I would like to show any of the content. I would also like to generate the thumbs automatically with php in the script. Is this possible ?
Regards Vaughan
fromvega
November 3rd, 2008 at 10:28 am
9Hello vaughan, it’s possible but you would need to write your own code for that since the script just generates XML.
RSS feed for comments on this post · TrackBack URI
Leave a reply