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

449 responses to “Creating XML files with DOM Document”

  1. frp-mad.com Avatar

    References:

    Steroid diet plan bulking

    References:
    https://buzzafricanmusic.com/alyciafranklyn

  2. https://beatsong.app/luis91h7073607 Avatar

    References:

    Anabolic steroids for sale usa

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

  3. 120.48.141.82 Avatar

    References:

    Can steroids affect your period

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

  4. www.fepp.org.ec Avatar

    References:

    Steroids legal in usa

    References:
    http://81.69.57.215:3000/maricruzamies

  5. 62.234.182.183 Avatar

    References:

    Steroid for muscle

    References:
    https://git.cjcrace.io/linfinley01186

  6. lafffrica.com Avatar

    References:

    What are steroids and what are they used for

    References:
    http://74.48.174.77:3000/norbertoskille

  7. 139.196.96.28 Avatar

    References:

    Steroid testosterone pills

    References:
    http://116.236.50.103:8789/merletafoya012

  8. 111.230.243.127 Avatar

    References:

    Synthetic hormone definition

    References:
    https://gitea.diputadosalta.gob.ar/sofiamachado08

  9. gitea.lasallesaintdenis.com Avatar

    References:

    How much testosterone should i take to build muscle

    References:
    http://z.duowenlvshi.com/nidav718108920

  10. https://aprileaton.net/martinabrain6 Avatar

    References:

    Is testosterone the same as steroids

    References:
    http://58.65.162.118:3000/meganjeffrey67

  11. 113.177.27.200 Avatar

    References:

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

    References:
    https://www.propitarios-alquilan.shop/renatorosetta0

  12. git.123doit.com Avatar

    References:

    Will one cycle of testosterone hurt you

    References:
    http://47.92.35.224:3000/hiltonbroadhur

  13. https://mycrewdate.com/ Avatar

    References:

    Short term steroid side effects

    References:
    http://120.201.125.140:3000/linneaday66197

  14. https://vidspaceaiapp.com Avatar

    References:

    Dianabol steroids for sale

    References:
    https://mycrewdate.com/@tonjawolak317

  15. 49.232.143.93 Avatar

    References:

    Weight loss steroids clenbuterol

    References:
    https://buzz.gi/@melindachun478?page=about

  16. 81.69.229.51 Avatar

    References:

    Steroid side effect

    References:
    http://gitea.mikarsoft.com/evelyne8560577

  17. nas.zearon.com Avatar

    References:

    Anabolic steroids side effects negative

    References:
    https://sysssl.tech/denicevillarea

  18. git.craigsau.dev Avatar

    References:

    Steroids to lose weight and get cut

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

  19. cameotv.cc Avatar

    References:

    Superdrol reviews

    References:
    http://47.103.159.168:10012/anyapesina004

  20. http://223.108.157.174/ Avatar

    References:

    What are steroids side effects

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

  21. git.youkehulian.cn Avatar

    References:

    Weight loss steroids for men

    References:
    https://jom.gg/@lljmarc0862713

  22. edexxel.com Avatar

    References:

    Types of steroids for muscle building

    References:
    http://38.47.180.165:3000/mavissylvia855

  23. romancefrica.com Avatar

    References:

    Shrinking pills height

    References:
    https://62.234.182.183/felipa5968226

  24. git.gotrobotics.org Avatar

    References:

    Anabolic steroid dianabol

    References:
    http://106.55.174.214:3000/gracielawerthe

Leave a Reply

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