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

807 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

  16. sundaynews.info Avatar

    References:

    Bedste slots med høj udbetaling

    References:
    https://newmuslim.iera.org/members/friendwool53/activity/555127/

  17. https://tsnasia.com Avatar

    References:

    Online casino dansk tilladelse høj udbetaling

    References:
    https://portal.shcba.org/employer/online-casino-bonus-2026-die-besten-aktionen/

  18. https://music.1mm.hk/lesnah2749752 Avatar

    References:

    Casinoer med høj RTP og store bonusser

    References:
    http://www.mikarsoft.com/louveniae71329

  19. sundaynews.info Avatar

    References:

    Casinoer med rekordhøje udbetalinger

    References:
    http://www.bbsls.net/space-uid-1808826.html

  20. roadwiki.site Avatar

    References:

    Bedste udbetalingsprocent på mobil casino

    References:
    http://x.kongminghu.com/home.php?mod=space&uid=431876

  21. hiwifi.denq.us Avatar

    References:

    Casinoer med højeste tilbagebetaling 2024

    References:
    https://git.hilmerarts.de/averycardella

  22. qomplainerzschool.lima-city.de Avatar

    References:

    Vind penge på casino med høj udbetaling

    References:
    http://daojianchina.com/home.php?mod=space&uid=1065905

  23. gitea.syn-assist.fr Avatar

    References:

    Gennemsigtige casino udbetalingsrater

    References:
    http://tfjiang.cn:32773/danniekennemer

  24. classifieds.ocala-news.com Avatar

    References:

    Casino spil med bedste udbetalingschancer

    References:
    http://www.jzq5.cn/space-uid-342045.html

  25. http://hkeverton.com Avatar

    References:

    Casinoer med bedste udbetalingsrate

    References:
    https://cq.x7cq.vip/home.php?mod=space&uid=9541437

  26. www.blurb.com Avatar

    References:

    Bedste danske casinoer med høj tilbagebetaling

    References:
    https://xs.xylvip.com/home.php?mod=space&uid=4463840

Leave a Reply

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