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

463 responses to “Creating XML files with DOM Document”

  1. jomowa.com Avatar

    References:

    Which of the following conditions is commonly associated
    with the abuse of anabolic steroids?

    References:
    http://112.74.106.216:3000/phojerome40467

  2. 49.232.183.190 Avatar

    References:

    Bodybuilders without steroids

    References:
    http://43.139.2.237:3000/oliversherlock

  3. gitea.scivigi.com Avatar

    References:

    Muscle building supplements review

    References:
    https://spinvai.com/jeramysainthil

  4. http://43.139.240.37/ Avatar

    References:

    Built mass phase 3 prohormone

    References:
    https://git.lolpro11.me/olamacdougall1

  5. git.ccmhub.se Avatar

    References:

    Winstrol v pills

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

  6. http://221.226.25.34/ Avatar

    References:

    Prolab pharmaceuticals steroids

    References:
    https://git.bp-web.app/manieklv20940

  7. https://clipsshort.com Avatar

    References:

    Steroids that make you lean

    References:
    http://124.71.197.109:3000/moniquenord742

  8. gitea.biboer.cn Avatar

    References:

    Steroid alternatives gnc

    References:
    https://git.thoughtvectors.uk/ezequielmcguir

  9. 61.190.74.90 Avatar

    References:

    How safe are steroids

    References:
    https://hlumi.com/@tobiasradke52?page=about

  10. 175.27.229.211 Avatar

    References:

    Best legal steroids reviews

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

  11. 49.232.183.190 Avatar

    References:

    Anabolic steroids reviews

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

  12. 120.48.141.82 Avatar

    References:

    Is steroids legal

    References:
    https://www.heyne.cloud/angelapbk25349

  13. music.1mm.hk Avatar

    References:

    Using testosterone for muscle gains

    References:
    https://git.davisdre.com/fredriccheyne

  14. 47.93.0.44 Avatar

    References:

    Steroids medical use

    References:
    https://musixx.smart-und-nett.de/judy803936542

  15. 120.201.125.140 Avatar

    References:

    Best steroid for athletic performance

    References:
    https://git.veraskolivna.net/denicestolp07

  16. http://47.107.188.236/ Avatar

    References:

    Supplement that burns fat and builds muscle

    References:
    http://111.230.9.98:3000/yasminrobledo

  17. http://www.scserverddns.top:13000/augustus00v096 Avatar

    References:

    Which body type is more common in men and
    associated with the most negative health risk?

    References:
    https://git.autotion.net/tatianaheaton8

  18. vidspaceaiapp.com Avatar

    References:

    Beginner steroid cycle

    References:
    https://dianyanai.com/dyanfranco4143

  19. gitea.nongnghiepso.com Avatar

    References:

    Winstrol pills results

    References:
    https://adufoshi.com/adamxpg8073949

  20. gogolive.biz Avatar

    References:

    What is the most powerful steroid

    References:
    https://daterondetjolie.fr/@elverawiley95

  21. git.slegeir.com Avatar

    References:

    Steroids muscle building

    References:
    https://tcodpractice.com/quintondorsett

  22. 117.72.61.28 Avatar

    References:

    Anabolic steroids uk

    References:
    http://121.36.47.159:3000/caloren4196740

  23. 171.244.15.53 Avatar

    References:

    Buy illegal steroids online

    References:
    http://47.98.183.226:3000/rudolffreeman1

  24. https://dianyanai.com Avatar

    References:

    Effects of steroids on athletes

    References:
    https://sportsdungeon.com/@lester45t53385?page=about

  25. https://gitea.primecontrols-dev.com/ Avatar

    References:

    Buying injectable steroids online

    References:
    https://89.58.50.249:8443/ezekielbittner

  26. https://gitea.savethemall.myds.me Avatar

    References:

    What is the function of steroids in the human body

    References:
    https://codes.tools.asitavsen.com/jakew826822686

  27. omegat.dmu-medical.de Avatar

    References:

    Anabolic steroid abuse side effects

    References:
    https://spice.blue/@dan69143967596?page=about

  28. www.musicsound.ca Avatar

    References:

    Legal injection steroids

    References:
    https://nauticauruguay.com/mariano41w8418

  29. git.rmarl.in Avatar

    References:

    Arnold schwarzenegger steroid cycle

    References:
    https://jomowa.com/@phoebevzq87373

  30. git.apextoaster.com Avatar

    References:

    Testosterone as a steroid

    References:
    https://lius.familyds.org:3000/luciosperling

  31. funnyutube.com Avatar

    References:

    Steroidsonline.co review

    References:
    http://git.youkehulian.cn/ralffik7704828

Leave a Reply

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