XML and PHP Simplified - Formatting XML Documents / Page 2 | WebReference

XML and PHP Simplified - Formatting XML Documents / Page 2


XML and PHP Simplified - Formatting XML Documents [con't]

In this section we will look at:

  • The Document Object Model or the DOM as it is commonly known
  • the XML functions that are available in PHP5.

The Document Object Model (DOM) and XML

What is the DOM? It can simply be described as a hierarchical model that is used to manipulate or to interact with documents. It does this by accessing parts of the document directly via the hierarchical structure. This makes the DOM ideal for manipulating XML contents because it goes so much further than the traditional PHP string functions that we have been using so far, not to mention the control it gives us. For example, we can access a particular tag of an XML document and even change its name! This is achievable because all XML documents have a specific relationship among its parts. For example, each XML document has (and can only have) one root element; this makes it the parent of all the rest of the elements in that document. If you think of a tree, the root is always at the bottom and the rest of the tree features comes from it. The DOM is a programmatical representation of a tree; so is an XML document.

There is a very specific terminology that is used when it comes to the DOM. Any given element, which has elements inside is called the root or parent element. The elements that are inside the root element are referred to as its children or child elements. So thinking of elements as parents and children is perfectly valid. You may have heard the term node when there is talk of the DOM, all components and elements are usually referred to as nodes.

The DOM has many functions most of which has been implemented by PHP in the form of the PHP DOM extension. We will not be discussing all of the functions but only the more commonly used ones. What we are interested in are functions that deal with creating, accessing and removing nodes in an XML document and also creating and using already existing XML documents. So let's take a look at the functions that create an XML document. To create a document:

The above line of code simply creates a DOM object inheriting all of the attributes and functions that are stored in the DOMDoucmant class. Now we can use this new object to add elements, attributes and child elements to the document:

When run, the code produces the following XML document:

I've used a number of new functions. So lets take a look at them:

  • createElement() – This function creates a new element. Depending on where you use it, it can be used to create a root element or a sub element
  • createTextNode() – Creates a new text node. The function returns the new Domtext or False if an error occurs
  • appendChild() - Adds new child element at the end of the children
  • save() – Creates a new XML document ( file), using the string used in the DOM. Returns the number of bytes written otherwise FALSE if an error occurs

We used a limited number of functions here. There are many more functions available, which we will take a brief look at in a bit. The DOM object also includes very useful errors. Rewrite the above code like so:

Then run it again. You should get an error that looks something like this:

I've highlighted the line that caused the error. What we've done is to try to create two root elements and that has caused the program to ask for clarification.

Now, let's look at the code step by step. First, we create an object called $doc that will inherit from the DOM:

Then we format the output. In this case, the output is the XML content:

Now we come to the meat of the program. We set the root element. This will create the element tags:

By using the appendChild() function above we ensure that the root element is actually shown and later on written to the XML document. Next, we create the child element called name, which creates the tags. Notice that we use the appendchild() functions to indicate that this element is a child of the root element (contacts) :

Now we set the text for the name element that we created above:

We do the same for the email and address elements. Both of them use the root element as the child element to link to the root:

XML functions in PHP5

Many of the XML functions in PHP5 are based on the libxml2. If you have PHP5 installed then simplexml is automatically turned on, meaning that you do not have to enable any additional extensions. To make double sure that the simpleXML extention is enabled in your version of PHP simply add the following code to a PHP document and run it:

Look for the simplexml extension. An added advantage is that the XML functions in PHP5 also support XML document validation when referencing a DTD or XML Schema. So let's take a look at the new simpleXML extentions:

simplexml_load_file: takes a file path as an argument, and if the contents of the file are well-formed XML, will load the contents as an object.

simplexml_load_string: takes a string as an argument, and the string should be well-formed XML. Converts the string to an object.

simplexml_import_dom: takes a node from a DOM document and turns it into a simplexml node.

simplexml_element->asXML: returns a well-formed XML string from a simpleXML object.

simplexml_element->attributes: provides the attributes and values defined within a well-formed string of XML.

simplexml_element->children: method provides the child elements of an element.

simplexml_element->xpath: method runs an XPath query on a simpleXML node.

The sample program that we created in this article shows how easy it is to use the XML functions and how these functions give us unlimited access to each element of an XML document and therefore making manipulation of the elements even easier. In the next article, we will take a further look at the DOM and the functions offered by PHP5.

Download the files for this series.

Original: August 5, 2009