XML in PHP 5

Mark Fenoglio

3 min read

Nov 17, 2005

XML in PHP 5

Now that PHP 5 is becoming more and more prevalent over its predecessors, some really good books that cover PHP need revisions. Nowhere is this more true than with XML-related topics, where PHP 5 has a completely different way of working with XML – and, in fact, the mechanisms from PHP 4 will no longer work.

In this article, we’ll be looking at one way to build and parse an XML tree using the new methods in PHP 5.

XML Review

As a (very) quick reminder about what XML is, you work with tags much like you do in HTML, but with two very important distinctions:

  1. (restriction) You must close every tag

  2. (benefit) You get to define the name of these tags in your XML document

In other words, what you are doing is creating a “tree” of XML entities where you are defining the data structure for the objects you want to manage (or allow others to manage).

By way of example, you might have the following XML file represent the teams in Major League Baseball.

<?xml version="1.0" encoding="iso-8859-1"?>
		<name>Atlanta Braves</name>
		<stadium>Turner Field</stadium>
		<name>Chicago Cubs</name>
		<stadium>Wrigley Field</stadium>


		<name>Baltimore Orioles</name>
		<stadium>Camden Yards</stadium>



Creating XML in DOM

The general workflow for creating an XML document using DOM is (where Steps 2 and 3 are repeated to complete the XML):

  1. Create a new DOM document

  2. Create elements

  3. Append children

In our example (considering only the first two teams), the code would be:


// xml_teams_dom.php

// create the new XML document
$dom = new DOMDocument('1.0', 'iso-8859-1');

// create the root element
$list_of_teams = $dom->createElement('teams');

// create the first team element
$team = $dom->createElement('team');

// now create all the subelements for the team
$name = $team->appendChild($dom->createElement('name'));
$name->appendChild($dom->createTextNode('Atlanta Braves'));

$stadium = $team->appendChild($dom->createElement('stadium'));
$stadium->appendChild($dom->createTextNode('Turner Field'));

$league = $team->appendChild($dom->createElement('league'));

// create the second team element
$team = $dom->createElement('team');

// now create all the subelements for the second team
$name = $team->appendChild($dom->createElement('name'));
$name->appendChild($dom->createTextNode('Chicago Cubs'));

$stadium = $team->appendChild($dom->createElement('stadium'));
$stadium->appendChild($dom->createTextNode('Wrigley Field'));

$league = $team->appendChild($dom->createElement('league'));

$xml_result = $dom->saveXML();

// simple mechanism to see the XML

print <<<XML_SHOW
<textarea cols='80' rows='40' name='xml_result'>$xml_result</textarea>


XML Parsing

Now that we have an XML structure, how exactly do we interrogate it for information?

The general workflow for parsing an XML document for specific elements is:

  1. Create a new DOM document

  2. Load an XML tree into the DOM document

  3. Query the new XML tree and loop through the desired elements

In our case, we might add the following code to our script:

// pull in the work we've just done
$team_xml = new DOMDocument();

// statement block to get at the elements of the xml document
$team_list = $team_xml->getElementsByTagName('team');
foreach ($team_list AS $team) {
	foreach ($team->childNodes AS $item) {
		print $item->nodeName . " = " . $item->nodeValue . "<br />";

This obviously only scratches the surface of using XML in PHP 5. XML is an extremely important tool in PHP (and everywhere else), particularly in the development of web services.

Zack Simon

Reviewer Big Nerd Ranch

Zack is an Experience Director on the Big Nerd Ranch design team and has worked on products for companies ranging from startups to Fortune 100s. Zack is passionate about customer experience strategy, helping designers grow in their career, and sharpening consulting and delivery practices.

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News