Creating XML files with DOM Document
As a first blog post I would like to have some attention for the fact that a lot of programmers are using functions and classes for doing exactly the same as what can be accomplished by using default PHP extensions.
XML files are very common nowadays and every developer needs to deal with them one day. Most of the scripts and classes used to generate XML files string use concatenation and echo to buildup XML files. Although this is a very short and quick method for simple XML files the same approach is often used with more complex XML files with namespaces. The code then often becomes bloated, unreadable and slow, so why not use the PHP 5 DOM API instead (or if you travelled from the past and still use PHP 4 try DOM XML). A quick example of the PHP 5 DOM API usage below.
An example XML:
1
Using DOM Document
03-12-2009
To generate this XML in an object oriented way we can use the following code:
// Define content of each event
$aEventNodes = array(
'id' => 1,
'title' => 'Using DOM Document',
'datetime' => time());
$document = new DOMDocument('1.0', 'UTF-8');
// Create the root
$root = $document->createElement('events');
$document->appendChild($root);
$eventElement = $document->createElement('event', '');
foreach($aEventNodes as $sKey=>$sValue) {
$eventElement->appendChild($document->createElement($sKey, $sValue));
}
$root->appendChild($eventElement);
// Output the XML file
echo $document->saveXML();
First we create a document object, we add the XML version and encoding as arguments. Note on the encoding is that this encoding is only used in the XML header, not in values we will later add to our XML so you still need to use utf8_encode for example. Now that we have a document setup we can start appending elements to it. By calling saveXML() we will get a XML three dump as string.
Like I said this is a very simple example, the API is capable of much more.
itstitle
excerptsa
MyBlog
14 Aug 25 at 1:21 pm
commander kamagra en ligne canada
vente de kamagra sans ordonnance
generique kamagra pharmacie au rabais comtat venaissin
17 Aug 25 at 10:15 am
cheap enclomiphene cost tablet
ordering enclomiphene generic a canada
buy cheap enclomiphene generic enclomiphene
17 Aug 25 at 10:31 am
androxal shipped cod
buy cheap androxal generic pharmacy in canada
buy cheap androxal no prescription online
17 Aug 25 at 11:59 am
ordering flexeril cyclobenzaprine canada no prescription
Where can i buy flexeril cyclobenzaprine no perscription
cheapest buy flexeril cyclobenzaprine purchase australia
17 Aug 25 at 12:49 pm
dutasteride prescription from doctors online
buying dutasteride generic does it work
buy cheap dutasteride purchase online uk
17 Aug 25 at 1:31 pm
will cialis or gabapentin show up in a drug test
buy cheap gabapentin cheap drugs
get gabapentin usa cheap
17 Aug 25 at 3:14 pm
online order fildena australia generic online
ordering fildena price in us
discount fildena generic no prescription
17 Aug 25 at 3:31 pm
buying itraconazole australia cheap
cheap itraconazole uk online pharmacy
discount itraconazole cheap online pharmacy
18 Aug 25 at 3:46 am
ordering staxyn us pharmacies
low price staxyn without prescription
buying staxyn uk cheapest
18 Aug 25 at 4:55 am
discount avodart ireland over the counter
order avodart generic alternatives
how to buy avodart cheap real
18 Aug 25 at 5:06 am
buy xifaxan without prescription pay cod
ordering xifaxan generic release date
cheap xifaxan cheap canadian pharmacy
18 Aug 25 at 6:06 am
buy rifaximin generic online mastercard
discount rifaximin generic is it legal
order rifaximin price in canada
18 Aug 25 at 7:00 am
koupit kamagra online uk
kamagra zásilkový obchod singapore
kamagra na prodej
18 Aug 25 at 9:41 am