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

647 responses to “Creating XML files with DOM Document”

  1. megastream.pl Avatar

    References:

    Eldorado casino

    References:
    https://git.clubeye.net/fannietroiano6

  2. unired.zz.com.ve Avatar

    References:

    Casino club chicago

    References:
    https://www.propose.lk/@caitlynmicklem

  3. https://asiannearby.com Avatar

    References:

    Downstream casino joplin mo

    References:
    https://dgwork.co.kr/antoniettay37

  4. play.ophirstudio.com Avatar

    References:

    Werken bij holland casino

    References:
    https://www.kornerspot.com/@estelablaubaum

  5. git.123doit.com Avatar

    References:

    Valley forge casino

    References:
    https://www.mein-bdsm.de/@nellyvalladare

  6. ott.saikatinfotech.com Avatar

    References:

    Cherokee casino tulsa

    References:
    https://gitea.gimmin.com/dustyaeh08140

  7. eduback.com Avatar

    References:

    Online sport betting

    References:
    https://friztty.com/@codyfinnan9864

  8. stukguitar.com Avatar

    References:

    Online gaming sites

    References:
    https://katambe.com/@ninachristian2

  9. git.warze.org Avatar

    References:

    Montecito casino

    References:
    https://maru.bnkode.com/@millieripley51

  10. iraqitube.com Avatar

    References:

    Slot machine app

    References:
    https://gitea.wuyuank.com/qerjenna519200

  11. git.daoyoucloud.com Avatar

    References:

    Comanche red river casino

    References:
    http://dsmcase.com:2180/annbogen26183

  12. www.herzog-it.de Avatar

    References:

    Wild rose casino emmetsburg

    References:
    https://tunelifystream.com/romainehoolan

  13. www.ikaros.asia Avatar

    References:

    Monkey quest games

    References:
    https://124.70.144.1/madelinefewing

  14. gitiplay.com Avatar

    References:

    Napoleons casino hull

    References:
    http://app.venusroyale.date/@markusspillman

  15. https://www.monasticeye.com/ Avatar

    References:

    Sands casino bethlehem pa

    References:
    https://mginger.org/@loriecourtney

  16. git.adityagupta.dev Avatar

    References:

    Rivers casino chicago

    References:
    https://worship.com.ng/haihupp2733380

  17. skapa ett binance-konto Avatar

    Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.com/tr/register?ref=MST5ZREF

Leave a Reply

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