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

551 responses to “Creating XML files with DOM Document”

  1. i10audio.com Avatar

    References:

    Steroids for cutting

    References:
    http://crontab.club:30015/miguel92275687

  2. https://git.esen.gay/ Avatar

    References:

    Abuse of anabolic steroids

    References:
    http://47.100.208.160:51300/conniegrover77

  3. gitlab.iplusus.com Avatar

    References:

    How are anabolic steroids dangerous

    References:
    http://120.211.66.170:8418/stuartmusselma

  4. http://1.13.196.248/ Avatar

    References:

    The adverse effects of using anabolic steroids are serious because

    References:
    http://34.17.182.140/jeroldmjn22817

  5. archea.dev Avatar

    References:

    Another name for physiological risk is safety risk.

    References:
    http://8.138.192.83:39639/maniebni296790

  6. https://git.lucas-michel.fr/ Avatar

    References:

    Doctor prescribed steroids

    References:
    https://archea.dev/charli19869226

  7. https://camtalking.com Avatar

    References:

    Gnc cutting supplements

    References:
    https://git.flymiracle.com/helainemcdouga

  8. vcs.cozydsp.space Avatar

    References:

    Anabolic performance

    References:
    http://20.249.208.89:3000/shennar5248400

  9. 43.139.2.237 Avatar

    References:

    Natural steroid alternatives

    References:
    https://www.chembans.com/@louis19s657530

  10. syq.im Avatar

    References:

    Buy anabolic steroids online usa

    References:
    http://47.104.60.158:7777/jonahnxc386753

  11. https://koseongnam.com Avatar

    References:

    Kai greene before steroids

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

  12. datingmywish.com Avatar

    References:

    Best post cycle therapy for steroids

    References:
    https://eduxhire.com/employer/free-testosterone-levels-by-age-pg-ml/

  13. https://qflirt.net/ Avatar

    References:

    Will one cycle of testosterone hurt you

    References:
    https://dianyanai.com/elbertmilliken

  14. https://datingmywish.com/ Avatar

    References:

    The clear anabolic series

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

  15. code.hpswk.com Avatar

    References:

    List of steroids and what they do

    References:
    https://mygit.kikyps.com/lashawnmarriot

  16. 114.247.226.83 Avatar

    References:

    Is dbol legal in the us

    References:
    https://mp3diary.com/suzettegepp36

  17. xeroworld.team Avatar

    References:

    Best steroids to take

    References:
    https://music.wzsipku.cn/erlindapaton95

  18. www.tacticallysolved.com Avatar

    References:

    Best bulking steroid

    References:
    https://git.vezpi.com/charmainschill

  19. 121.41.95.54 Avatar

    References:

    Anabolic steroids classification

    References:
    http://gsoot.co.kr/bbs/board.php?bo_table=sub07_02&wr_id=39186

  20. 43.142.166.108 Avatar

    References:

    Jay cutler steroids cycle

    References:
    https://gogolive.biz/@meaganhhl37783?page=about

  21. 171.244.15.53 Avatar

    References:

    Bodybuilding gear steroids

    References:
    https://bfreetv.com/@shawneey852759?page=about

  22. git.archieri.fr Avatar

    References:

    Best steroids for muscle growth

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

  23. www.robots.rip Avatar

    References:

    Safe steroids for bodybuilding

    References:
    https://gitee.planhomecloud.cn/jessikamaio758

  24. reruberrypi.rerurate.com Avatar

    References:

    Where do anabolic steroids come from

    References:
    https://git.econutrix.com/uwbmattie98420

  25. 27.223.88.102 Avatar

    References:

    What steroids to take

    References:
    http://152.42.207.183:3000/zdpbritney1971

Leave a Reply

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