Using XML with PHP

Asif Ahmed

Often times, developers will come to a point where they have to use PHP to generate XML. Using PHP to generate XML isn’t exactly the funnest job, but it is a lot better than “hand generating” XML by string concatenation. Note you should never generate XML by string concatenation. It is buggy, prone to mistakes, less maintainable, and honestly, it’s just slower. As we start to research how to generate XML with PHP, we find the task to be a little bit daunting. There are like almost 10 PHP libraries that deal with XML manipulation! Here are all the one that are listed on the official PHP documentation. So how do we go about choosing which one? There’s actually a lot of Stack Overflow questions on this topic! After filtering through all the questions and answers, I’ve found that the two that work the best for me are SimpleXML and the DOM. I’ve found that SimpleXML is a lot simpler to use (hence the name), but when you need all the options (CData strings) it is best to use the DOM. Both of these PHP extensions should be enabled by default and installed if you are running any modern version of PHP, so you shouldn’t have to worry about that. Let’s explore both these extensions and see some sample XML generated by each.

This is the XML we want to generate by each extension.

<player team="bulls" championships="6">Michael Jordan</player>
<player team="celtics" championships="11">Bill Russell</player>
SimpleXML is really straightforward. You basically add children nodes to parent nodes and attributes to different nodes. Here is how you would generate the above XML using SimpleXML

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'.'<sports></sports>');

$basketball = $xml->addChild('basketball');
$greatest = $basketball->addChild('greatest_players');

$jordan = $greatest->addChild('player','Michael Jordan');

$russell = $greatest->addChild('player','Bill Russell');

// print out xml in browser
Header('Content-type: text/xml');

The DOM PHP generation is a little bit more complicated – you have to create elements and attributes and once you have created them then you can append them to the DOM

$xml = new DOMDocument('1.0','UTF-8');
$root = $xml->createElement('sports');

$basketball = $xml->createElement('basketball');

	$greatest = $xml->createElement('greatest_players');

	$jordan = $xml->createElement('player','Michael Jordan');
		$team = $xml->createAttribute('team');
		$team->value = 'bulls';
		$rings = $xml->createAttribute('championships');
		$rings->value = '6';

	$russell = $xml->createElement('player','Bill Russell');
		$team = $xml->createAttribute('team');
		$team->value = 'celtics';
		$rings = $xml->createAttribute('championships');
		$rings->value = '11';




// print out xml in browser
Header('Content-type: text/xml');

The biggest difference I would say is that SimpleXML allows for a top down (intuitive) approach where you can add children right away to XML. In DOM you have create the children fully first before you can append them to the parents. So SimpleXML is kinda like where parents birth their children and DOM is where children are adopted by their new parents (#badanalogy). However, DOM does allow for things like creating CData elements. Look at the differences between the two examples and figure out what works for you!
  • Nauru wakacje

    Thank you for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such fantastic info being shared freely out there.

  • Rahul Sati

    good example.. easy to understand..