XML and PHP Simplified: The DOM | WebReference

XML and PHP Simplified: The DOM

By Leidago Noabeb


[next]

In this article, we will take a further look at the XML functions that are offered by PHP5. At the same time, we will also look at some of the other DOM functions that deal with the Document Object Model.

There are three main DOM classes defined in the DOM API that is included with PHP5:

  • DOMElement – Enables you to create and manipulate any aspect of an element
  • DOMNode - Enables you to create and manipulate any aspect of a node
  • DOMXP – Enables work with, among other things, namespaces.

There are others such as the DOMComment,DOMAttr etc, but for the sake of brevity we are only going to focus on the ones that are going to help us the most. In the previous article, we have been using a mix of functions from all the classes except the DOMXP class. So what else can we do with the DOM? So far, we've created whole XML documents using the DOMElement and DOMNode classes. However, what if we want to access these elements and text nodes that we have created? The DOM allows us to do that as well. It provides the following functions for that:

DOMNode->removeChild(‘oldchildnode') — Removes child from list of children. The function takes one parameter and returns the old child node if it fails. Let's try the following code to demonstrate how to use the function. We use the code that created an XML document for us previously. Then we use removeChild() in the section listing of code:

The newly created document now looks like this:

Notice that in this XML document we have three names and that there is a hierarchical system in place in which the names have the following order:

Eno (0)
Simon (1)
Xuro(2)

So if we want to remove Eno from the DOM string, then we do the following, using removechild():

If you look at the code above, first we create a new object of the document, since we are working with an XML document:

$doc = new DOMDocument;

Then we load the document into the object:

$doc->load('myxml.xml');

Next, we create an object of the document element; this is because we want to remove the element in the document:

$contact = $doc->documentElement;

So all of the elements information of the document is now stored in the $contact object. Now we want to retrieve the contact that is stored at point zero-. This contact is called eno and is first on the element list stored at place number zero:

$contname = $contact->getElementsByTagName('contact')->item(0);

Then we remove it:

$oldcontact = $contact->removeChild($contname);

To see the results of our work, we echo out the remaining pieces of the document. Remember that we are working with the string representation of the XML document, so even if we call the function below, the file itself will not be modified until you call the save() function:

echo $doc->saveXML();

When executed the results should be:

What if you just want to replace the child node? The DOM provides a function for that too:

DOMNode->replaceChild() — Replaces a child. The function takes two parameters and returns false if an error occurs. Look at the following code:

It gives the following result:

You can see that the name Eno has been replaced by !Aribasen Seibeb. The DOM gives us almost complete control over an XML document and its various parts. We can go on and on until we cover all of the functions; instead we will later write a program that will show even more functions that we haven't covered here.

XML functions in PHP5

The simpleXML extention We already had a peek at some of the functions that are available to PHP5 that help to manipulate its data. The simpleXML extension is installed by default in PHP5 and provides, like the DOM, a simpler way to manipulate XML data. Like the DOM, it enables us to convert an entire XML document into an object. Once it is an object you can do whatever you want with it, as you would with any other object. Below is a list of some of the functions: 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.

So, you have a XML file and want to load it into an object so you can work with it. What do you do? You use the load_file() function. By doing this you can pretty much manipulate the elements and attributes much like an array. To demonstrate, let's use our XML document with this function. It has the following contents:

Below is the code to demonstrate its use:

First we check if the file exists:

if (file_exists('myxml.xml')) {

Then we convert the XML file into an object and store its contents into the $obj object and print them to the screen:

$xml = simplexml_load_file('myxml.xml');
print_r($xml);

When you run this code, you should get something like this:

You can now access all of the elements and attributes using simplexml functions.


[next]