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

785 responses to “Creating XML files with DOM Document”

  1. https://hedgedoc.info.uqam.ca/s/ulbgKaHjL Avatar

    References:

    Hurtige udbetalinger og høj RTP

    References:
    https://doc.adminforge.de/s/w67BY8FuHt

  2. fkwiki.win Avatar

    References:

    Hvilke online spil har høj udbetaling

    References:
    http://szw0.com/home.php?mod=space&uid=1010082

  3. truckwiki.site Avatar

    References:

    Online casino med største udbetaling

    References:
    https://intensedebate.com/people/brazilrussia5

  4. techou.jp Avatar

    References:

    Casinoer med bedst udbetaling på bordspil

    References:
    http://hkeverton.com/forumnew/home.php?mod=space&uid=639716

  5. http://fujiapuerbbs.com Avatar

    References:

    Online casino med høj udbetalingsrate

    References:
    https://pads.jeito.nl/s/m4jUIrQhCy

  6. http://www.pshunv.com/ Avatar

    References:

    Online casino udbetalingsstatistik

    References:
    https://www.giveawayoftheday.com/forums/profile/1833372

  7. isowindows.net Avatar

    References:

    Største casino udbetalinger i Danmark

    References:
    http://adrestyt.ru/user/holeloaf07/

  8. git.hubhoo.com Avatar

    References:

    Casino bonus uden indbetaling høj udbetaling

    References:
    https://x1.tvos.cygnux.cn/ezra9713441632

  9. suprasage.com Avatar

    References:

    Spillemaskiner med bedste udbetalingsprocent

    References:
    http://dev-gitlab.dev.sww.com.cn/briannei594991

  10. notes.medien.rwth-aachen.de Avatar

    References:

    Live dealer casino høj udbetaling

    References:
    https://is.gd/3M9fXm

  11. argrathi.stars.ne.jp Avatar

    References:

    Casinoer med højeste udbetalingsprocent

    References:
    http://bbs.xingxiancn.com/home.php?mod=space&uid=890993

  12. git.huwhy.cn Avatar

    References:

    Vejledning til højeste casino udbetaling

    References:
    https://classifieds.ocala-news.com/author/carlosconde

  13. winesandjobs.com Avatar

    References:

    Optimal udbetaling på online casino

    References:
    https://k0ki-dev.com/deonbadham523

  14. gitea.kdlsvps.top Avatar

    References:

    Bedst betalende online casinoer

    References:
    https://xn--lpris-iua.nu/walkerbeeby173

  15. www.supergame.one Avatar

    References:

    Danske casinoer med bedste odds og udbetaling

    References:
    http://alfa-power.de/member.php?action=profile&uid=20163

Leave a Reply

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