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

697 responses to “Creating XML files with DOM Document”

  1. Binance注册 Avatar

    Your article helped me a lot, is there any more related content? Thanks!

  2. sostinestauras.lt Avatar

    References:

    Vind penge online casino høj udbetaling

    References:
    https://considerjesusministries.org/?page_id=603

  3. https://www.busforsale.ae/profile/precious512606 Avatar

    References:

    Casinoer der udbetaler hurtigst og mest

    References:
    https://links.gtanet.com.br/corrinemarmi

  4. x1.tvos.cygnux.cn Avatar

    References:

    Casinoer med høj udbetaling og godt omdømme

    References:
    https://git.wikiofdark.art/leannepermewan

  5. git.morozoff.pro Avatar

    References:

    Casinoer med højeste gennemsnitlige udbetaling

    References:
    https://gitea.myat4.com/otisderr38455

  6. https://git.powderbluecrayon.com/ Avatar

    References:

    Vind penge online casino høj udbetaling

    References:
    https://gitea.jsjymgroup.com/barryproud8924

  7. gitea.personalsoftware.space Avatar

    References:

    Bedste udbetalingsprocent på mobil casino

    References:
    https://gitea.amazingcoders.com/lynnramirez97

  8. http://git.520hx.vip/jannapauley668 Avatar

    References:

    Top casinoer med mest udbetaling

    References:
    https://gitlab.herzog-it.de/wilbertreimann

  9. phoebe.roshka.com Avatar

    References:

    Casino bonus uden indbetaling høj udbetaling

    References:
    https://git.auth.com.br/shermanmercer

  10. gitlab.dev.jtyjy.com Avatar

    References:

    Førende casinoer med høj udbetaling

    References:
    https://gitea.my-intrudair.com/nathanhuntingf

  11. git.datanest.gluc.ch Avatar

    References:

    Gennemgang af de bedste udbetalende casinoer

    References:
    https://gitea.kamisama.ovh/ikmbrad3910076

  12. zenithgrs.com Avatar

    References:

    Top casinoer med mest udbetaling

    References:
    https://incisolutions.app/marcolanning09

  13. https://git.clickforadventure.co/patrickz165485 Avatar

    References:

    Casinoer med høj udbetaling uden omsætningskrav

    References:
    https://body-positivity.org/groups/bonusser-spil/

  14. https://li1420-231.members.linode.com/hollis9249868 Avatar

    References:

    Fordelagtige udbetalingsregler casino

    References:
    http://git.dglyoo.com/bernadineteete

  15. gitea.brmm.ovh Avatar

    References:

    Maksimer dine gevinster på casino

    References:
    http://git.mynas71.myds.me/alfonzogolding

  16. jobcop.in Avatar

    References:

    Online casino med høj udbetalingsrate

    References:
    https://actsolution.iptime.org:3000/ralfmora666142

  17. https://git.alderautomation.ca Avatar

    References:

    Bedste danske spilleautomater med høj udbetaling

    References:
    https://git.sortug.com/bartl424103112

  18. mindepoch.com Avatar

    References:

    Gennemgang af de bedste udbetalende casinoer

    References:
    https://git.sunlix.one/isabellhannan

  19. https://gitea.brmm.ovh/ Avatar

    References:

    Sammenligning af casino udbetalinger

    References:
    https://git.clickforadventure.co/audrajefferies

Leave a Reply

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