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.

Comments

484 responses to “Creating XML files with DOM Document”

  1. 62.234.201.16 Avatar

    References:

    Buy illegal steroids online

    References:
    https://www.musicsound.ca/windyy66359721

  2. 110.42.217.153 Avatar

    References:

    Anabolic steroid use may cause all of the following side effects
    except

    References:
    https://kidstv.freearnings.com/@constance00703?page=about

  3. git.rmarl.in Avatar

    References:

    Did arnold schwarzenegger use steroids?

    References:
    http://47.92.35.224:3000/dongneale0733

  4. git.chilidoginteractive.com Avatar

    References:

    Do bodybuilders use steroids

    References:
    http://8.155.172.147:3001/markmarcus2950

  5. mkhonto.net Avatar

    References:

    Where to buy steroid injections

    References:
    https://homenetwork.tv/@charleneeasley?page=about

  6. 110.42.101.39 Avatar

    References:

    Anavar steroid buy online

    References:
    http://118.89.79.106/melody92x63655

  7. git.123doit.com Avatar

    References:

    Women bodybuilders on steroids

    References:
    https://music.1mm.hk/ambermobley758

  8. git.huwhy.cn Avatar

    References:

    What does winstrol do to your body

    References:
    https://chinami.com/@bryonrabin858?page=about

  9. git.clubeye.net Avatar

    References:

    Perfect steroid cycle

    References:
    http://81.70.30.91:3000/ceciliabagley4

  10. jinhon-info.com.tw Avatar

    References:

    Tren injection side effects

    References:
    https://iamtube.jp/@azcmaryellen85?page=about

  11. https://inmessage.site/ Avatar

    References:

    What does steroids do to your body

    References:
    https://dianyanai.com/nicholecarrol3

  12. volunteeri.com Avatar

    References:

    Best online steroid supplier

    References:
    http://66.179.208.56:3001/estellao853997

  13. crontab.club Avatar

    References:

    Best injectable testosterone for bodybuilding

    References:
    https://dreamplacesai.de/bridgettusi404

  14. 120.48.144.83 Avatar

    References:

    Steroids for the brain

    References:
    http://121.199.174.122:3000/ashleigh59170

  15. 62.234.194.66 Avatar

    References:

    What does anabolic steroids do to your body

    References:
    https://video.2yu.co/@bettinagreenup?page=about

  16. http://120.77.94.227/ Avatar

    References:

    Buying illegal steroids online

    References:
    https://m.my-conf.ru/frederickmccul

Leave a Reply

Your email address will not be published. Required fields are marked *