![]() |
Generating XML from a Mysql DB with PHP's DOM functions (part one)
Hello all,
This is gonna be (hopefully) part one of two about PHP and XML/XSL. I was inspired to do this tutorial after seeing Wildhoney's post about XML and XML's family of technologies and decided I'd have a crack at writing a tutorial on what i know. I was going to do a full article on how to generate an XML file then perform an XSL 'transformation' with PHP's XSL functions, but that would have taken too long to write all at once, and there is alot for you guys to take in. So i decided to just post this first, then when i get some time ill write the rest in part 2 (unless someone feels like they wanna do it ) In this article im going to briefly show you how to generate an XML file from a MySQL database using PHP's DOM functions. Im assuming that you know what XML is and how it works, read another good tutorial on Tizag if your not sure Just to note, im by no means an expert on this, but i do know enough to use it, and i wanted to show the power and flexibility it provides. Ok, enough of the waffling, ON WITH THE XML!!! The first thing we need is a database (for this example anyway, you dont 'need' a DB for this technology), heres how my mysql database table looks: mysql Code:
Now that we have our DB sorted we can now look at creating an XML file from it. Im not going to bother with fancy PHP DB classes or security, as this is mainly about XML/XSLT in PHP, you should be able to tailor it to your needs. First off we need to connect to the DB: PHP Code:
PHP Code:
PHP Code:
Notice i have passed '1.0' in to the construct, this means its going to create an XML version 1.0 file, there is another optional parameter that takes the charset i.e. UTF-8 etc but i cant be botherd :) Next im going to create a 'Root' element for our new XML file: PHP Code:
So basicly we have created a new element called 'carlist' then we called the appendChild() function, this functions appends a child to an existing list of children or creates a new list of children, in our case its our root node (once again you need to know XML to understand that) Next I'm going to fetch an array from the db, and do some more stuff that ill explain: PHP Code:
Ok the next two lines this look familiar dont they? thats because effectively they are, but this time we aren't creating a root, so appendChild() will append the new element to our root and it will become a child of the root, which in our case is 'carlist', so our XML structure will look like this (NOTE a XML document can only have ONE root): xml Code:
Next we need to add the rest of the elements, this time the data of each car, we do this by creating children of car. We can do this by nesting a foreach within the while and looping through the associative array produced by mysql_fetch_assoc(), like so: PHP Code:
We next create a new element and name it by the fieldname (from the array, which is from the field name in the database), then append it as a child of 'car', we use the reference to 'car' that was created in the while loop in the $car variable. Then we add some data from the database into the newly created node. So our xml file will look something like this: xml Code:
After that script has run, we end up with something that looks like this: xml Code:
PHP Code:
So there you have it, we have just created an XML file using PHP's DOM functions. There are some more advanced things we could have done, i.e. instead of having a separate node for 'name' we perhaps could have created an 'attribute' of the 'car' node with this: PHP Code:
xml Code:
xml Code:
Hopefully i explained enough there, if not just ask me and ill be only too happy to clarify something, or if i have got something horrifically wrong then I'm open to criticism. In the next part I'll show you how to perform a 'transformation' with a XSL file with PHP. Until then have fun EDIT: Part 2 and attachment with files:http://www.talkphp.com/showthread.php?p=4904 |
Good job m8, another good article. Especially looking forward to part two :)
|
Nice little article, I was going to write something similar but without using the DOM. One thing to point out, to save us tired fingers, is an easier/quicker method of attaching simple element structures (ie, a node with just some text) to the document.
sketchMedia's example (copied from above) PHP Code:
Everything will work the same, honest. It's just a personal preference but I like this approach better for simple elements. PHP Code:
|
Yea thats defiantly a more elegant way of typing it m8 thanks.
Part 2 almost done :) |
At least you're not doing it at 6am this time :-) Crazy time, if I might say so myself! I love how all DOM stuff is standardised - one of the very few areas where PHP is, perhaps? Makes it so much easier after years of Javascript DOM to actually get your head round.
|
indeed, i love this new fangled xml/xslt stuff in php, i love xpath too its all good :-).
Just about to post part 2 8-) |
hey, i just completed your tut because i need to get some data from php into flash, and so far i was doing it directly....php to flash and everything was fine but because i got a paging system in php the php changes all the time (i.e blabla.php?x=y) and in flash i've got to load the page which cannot happen if the page changes.....i saw your tut about making an xml and i know you can load an xml into flash so im going to give it a go.....but i get the following error when previewing the page.....
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Web Root\Test\TMP9mcbxywrkg.php on line 23 Wrote: 34 bytes my code is as follows: PHP Code:
|
hi m8, glad you liked it.
the problem (as far as i can see) as that the 'foreach' loop is outside of the 'while'. fixed: PHP Code:
|
Need some help, if possible.
i have some data from the DB and need to output it into an new xml doc like this Code:
<thumbnails>any idea, the output has to be identical, since it goes to an flash, and i´ve no idea, how to modify Action script |
good job like this topic
|
[http://www.nflcnshop.net],Nike NFL Jerseys,Wholesale NFL Jerseys, Cheap nba jerseys,Wholesale 2013 NBA All Star ,Nike NFL Jerseys,Football Jerseys Cheap,Cheap NFL jerseys, nfl jerseys from china,Cheap Nike NFL Jerseys,NFL Jerseys By Nike,Cheap Jerseys Wholesale,Buy Cheap Nike Jerseys,Wholesale MLB Jerseys,Cheap NFL Hats,jersey store, jersey wholesale,steelers jersey,packers jersey, jersey wholesale,new nike nfl jerseys,nfl jerseys for sale
|
| All times are GMT. The time now is 10:04 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0