Here is a new flavor of the XML Generator, called XML Generator DB. This version builds XML files based on records from a database table instead of files in a directory.
I was thinking about embedding this functionality into the original script (now called XML Generator DIR to follow the name pattern) but it would add a lot more complexity in its configuration. The usage is pretty similar to the original, the model is the same but with different placeholders.
If you need to list files from directories in a filesystem instead of a database you should check the XML Generator DIR instead.
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" ?> <categories table="{tablename}" date="{callback:today}"> <category> <id>{field:cat_ID}</id> <nome>{field:cat_name}</nome> <nice>{field:category_nicename}</nice> </category> </wp_categories>
This XML is a template of a one used for creating a list of categories. Look that we have the root tag categories and its child tag category. The XML Generator DB will detect it and will duplicate the child node for every record found in the database. The placeholders {…} will be replaced by their meanings.
This script supports four 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_db.php and edit from line 30 up to line 44. Here is what you will see:
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // name of the XML file $xmlFileName = 'dbrecords.xml'; // name of the XML model $xmlModelName = 'model_db.xml'; // database configuration - the DNS below is a sample for MySQL $db_dsn = 'mysql:host=localhost;dbname=mydatabase'; $db_user = 'username'; $db_pass = 'password'; // set the table to be read or build a custom query below $db_table = 'mytable'; // custom query (null select all table data) $db_query = null; |
In particular you should be careful while setting the DNS (line 36) of the database. You should inform the right string of your database as they may be a little different from the MySQL one. To make your life easier here are some examples of some popular databases:
mysql:host=localhost;dbname=mydatabase sqlite:/opt/databases/mydb.sq3 pgsql:dbname=example;user=nobody;password=change_me;host=localhost
You can call the script from two different ways. You can call it through the webserver from the browser as you would normally do.
http://localhost/xmlgen2_db.php
You can also execute it through the command line interface (CLI). If you execute the script through the CLI you gain the possibility of using the {input:} placeholder and interact with the processing.
$ php xmlgen2_db.php
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.
5 Responses
Matt
December 6th, 2007 at 12:31 am
1Could you please help me? I need to create an XML file that is filled randomly and so next time it is randomized again it overwrites the other data in the XML file. Please help. I am new and confused.
fromvega
December 6th, 2007 at 4:28 pm
2Matt, this script is designed to catch fields from database and insert into a XML file. I think it won’t help you to do what you want. You should read about SimpleXML in the PHP manual.
John
June 14th, 2009 at 3:45 pm
3IF there are blank fields in the database the opening tag in the xml is not produced if I put spaces in the data field no problem look at title and filename in this output.
1102
30
http://www.amillioncooks.com/data/images/player_logo.jpg
Finally, at the end of execution I get the following error.
———————————————-
fromvega XML Generator 2.DB - fromvega.com
———————————————–
Creating XML file…
Warning: DOMDocument::loadXML() [domdocument.loadxml]: xmlParseEntityRef: no name in Entity, line: 8 in /home/u6/amctsn2009/html/playlist_test.php on line 113
Warning: Invalid argument supplied for foreach() in /home/u6/amctsn2009/html/playlist_test.php on line 116
Done!
Love the script and can probably work through the issues jsut thought you might be able to give me a headstart.
Thanks for your help.
Ap.Muthu
September 6th, 2009 at 3:03 am
4set the DSN and not the DNS.
Quote: “In particular you should be careful while setting the DNS (line 36) of the database. “
z
November 5th, 2009 at 6:17 am
5fantastic! this made a bazillion things so much easier!
RSS feed for comments on this post · TrackBack URI
Leave a reply