Introducing SimpleXML in PHP 5

In the first article of a three-part series, you will learn the basics of using the “simplexml” extension that comes with PHP 5. This library is primarily focused on parsing simple XML files, and can save you a lot of work.

Introduction

For those of you who have been using PHP 5 for a while, the numerous improvements introduced into the Zend engine have brought significant advantages that help to accelerate the development of Web applications. Intermediate and experienced developers probably will tell you nice things about the new object model, and the implementation of exceptions, in addition to the set of functions aimed at processing XML data in different ways. And of course, the list of new features goes on and on.

However, recently I discovered a little jewel hidden behind the tons of big improvements that come with PHP 5: a small and powerful library called “simpleXML.” Indeed, if you’re currently working on a project that requires handling XML data in a basic manner, this extension might eventually suit your needs, without your having to develop a full-featured XML parser.

Definitely, one of the best things about “simpleXML” is how easily it performs  basic tasks, such as reading XML files, extracting data from XML strings, comparing and replacing nodes, and so forth. Based on this simplicity, you can develop a decent XML parsing application without having to make a huge effort, or even combine all these cool features with the functionality of AJAX, in order to construct a full-fledged application that uses the best of client and server-based scripting languages.

In response to all the exciting features that I mentioned before, in this three-part series I’ll be taking a close look at the most important functions of the “simpleXML” extension, using numerous code samples and clear explanations to put them in context, so you can understand how they work.

At the end of the series, you should be equipped with the proper background to start using “simpleXML” in your own PHP 5 applications.

Having now defined the goals of this series, let’s find out together how to use the “simpleXML” library with PHP 5. Let’s get going!

{mospagebreak title=Reading XML files with the “simplexml_load_file()” function}

In order to begin tasting the power of the “simpleXML” extension, I’ll start with a simple example, which shows how to read the contents of a basic XML file. This extension will allow you do this in different ways, but in this case, I’ll use the “simplexml_load_file()” function, in conjunction with the following sample XML file:

<?xml version=”1.0″ encoding=”iso-8859-1″?>
<users>
<user>
<name>John Doe</name>
<address>Binary Avenue 1234 FL</address>
<email>john@john-domain.com</email>
</user>
<user>
<name>Janet Smith</name>
<address>Crazy Bits Road 4568 CA</address>
<email>janet@janet-domain.com</email>
</user>
<user>
<name>James Smith</name>
<address>Socket Boulevard 7894 OH</address>
<email>james@james-domain.com</email>
</user>
<user>
<name>Silvia Wilson</name>
<address>Protocol Avenue 5652 NY</address>
<email>silvia@silvia-domain.com</email>
</user>
<user>
<name>Alejandro Gervasio</name>
<address>Boulevard of Objects 10101 AR</address>
<email>alejandro@alejandro-domain.com</email>
</user>
</users>

Fine, now that I have a simple XML file to work with, have a look at the following script, which first reads the nodes of this file and next displays them by using a “foreach” loop:

// displays all the file nodes
if(!$xml=simplexml_load_file(‘users.xml’)){
    trigger_error(‘Error reading XML file’,E_USER_ERROR);
}
echo ‘Displaying contents of XML file…<br />’;
foreach($xml as $user){
    echo ‘Name: ‘.$user->name.’ Address: ‘.$user->address.’
Email: ‘.$user->email.’<br />’;
}

As you can see, the previous script loads the contents of the XML file by using the “simplexml_load_file()” function and then loops over the corresponding nodes, outputting the following values:

Displaying contents of XML file…
Name: John Doe Address: Binary Avenue 1234 FL Email: john@john-
domain.com
Name: Janet Smith Address: Crazy Bits Road 4568 CA Email: janet@janet-domain.com
Name: James Smith Address: Socket Boulevard 7894 OH Email:
james@james-domain.com
Name: Silvia Wilson Address: Protocol Avenue 5652 NY Email:
silvia@silvia-domain.com
Name: Alejandro Gervasio Address: Boulevard of Objects 10101 AR
Email: alejandro@alejandro-domain.com

That was really easy, right? The cool “simplexml_load_file()” function loads the contents of a specific XML file passed as an argument onto an object, which exposes the different file nodes as properties. In order to access the nodes in question, all you have to do is use the “->” notation, and you’re done.

Now, let me go one step further and demonstrate how to access the contents of a single node. The example shown below teaches you how to display the contents of all the <name> nodes included within the previous XML file:

// displays contents of <name> nodes
if(!$xml=simplexml_load_file(‘users.xml’)){
    trigger_error(‘Error reading XML file’,E_USER_ERROR);
}
echo ‘Displaying user names of XML file…<br />’;
foreach($xml as $user){
    echo ‘Name: ‘.$user->name.’<br />’;
}

In this case, all the <name> nodes are accessed as object properties, and displayed as follows:

Displaying user names of XML file…
Name: John Doe
Name: Janet Smith
Name: James Smith
Name: Silvia Wilson
Name: Alejandro Gervasio

Definitely, you’ll have to agree with me that the “simpleXML” extension makes it really easy to parse XML files, at least when you only need to perform basic tasks on a given file, such as reading all of the file nodes or accessing a particular one.

However, there are still more useful things to explore regarding the use of the “simplexml_load_file()” function. Therefore, in the next section of this tutorial, I’ll show you how to access a specific node within an XML file by using an array notation. Please continue reading to learn more.

{mospagebreak title=Accessing file nodes as array elements}

As I said before, after loading an XML file with the “simplexml_load_file()” function, it’s possible to access file nodes as array elements, which makes searching specific nodes a no-brainer task. In order to demonstrate this excellent functionality, I coded below a basic example that shows how to locate a specific node within the XML file I used before, and also displays additional data. Please take a look at this code listing:

// locates a specific node and displays name of first user
if(!$xml=simplexml_load_file(‘users.xml’)){
    trigger_error(‘Error reading XML file’,E_USER_ERROR);
}
// displays first user’s name
echo ‘Displaying name of first user…<br />’;
echo $xml->user[0]->name;

As illustrated above, once the contents of the sample “users.xml” XML file have been loaded, the script locates the first user and displays his full name:

Displaying name of first user…
John Doe

As you can see, specific file nodes can be easily accessed by using a regular array syntax, as the line below demonstrates:

echo $xml->user[0]->name;

If you examine the above expression, you’ll understand how the first user is located (user[0]) and how that user’s full name is properly displayed (user[0]->name). As you’ll probably realize, the “simpleXML” extension allows you to locate a given node easily within an XML file, as you would with regular arrays, and then access other sub nodes.

Based on the same “users.xml” XML file that you saw previously, here is the correct expression for accessing the last <user> node of the respective file and  displaying its email address:

// locates a specific node and displays name of last user
if(!$xml=simplexml_load_file(‘users.xml’)){
    trigger_error(‘Error reading XML file’,E_USER_ERROR);
}
// displays last user’s email address
echo ‘Displaying email address of last user…<br />’;
echo $xml->user[4]->email;

Actually, the entire process for locating file nodes is reduced to navigating the three nodes, as illustrated in the previous example, so you shouldn’t have any problems accessing different nodes within a given XML file.

At this point, I’m pretty sure that you learned the basics of loading XML files and navigating across file nodes, since the “simpleXML” extension is very understandable. Considering this, in the next few lines I’ll build a simple PHP 5 class, which will behave as a simple wrapper for the “simplexml_load_file()” function, performing basic parsing of XML files. To find out how this will be achieved, please click on the link below and keep on reading.

{mospagebreak title=Creating a basic XML parsing class}

In order to parse XML files using an object-oriented approach, in this section I’ll build a simple PHP 5 class. It will use the “simplexml_load_function()” that you learned before as the workhorse for reading and processing XML data. This class will take, as a unique parameter, the name of the XML file that will be parsed, and its definition is as follows:

class XMLParser{
    private $xml;
    public function __construct($xmlFile=’default_xml_file.xml’){
        if(!file_exists($xmlFile)){
            throw new Exception(‘Invalid XML file.’);
        }
        // read XML file
        if(!$this->xml=simplexml_load_file($xmlFile)){
            throw new Exception(‘Error reading XML file.’);
        }
    }
    // fetch specific nodes according to node name
    public function fetchNodes($nodeName){
        if(!$nodeName){
            throw new Exception(‘Invalid node name.’);
        }
        $nodes=array();
        foreach($this->xml as $node){
            $nodes[]=$node->$nodeName;
        }
        return $nodes;
    }
    // fetch all nodes as array of objects
    public function fetchNodesAsObjects(){
        $nodes=array();
        foreach($this->xml as $node){
            $nodes[]=$node;
        }
        return $nodes;
    }
    // count nodes of type $nodeName
    public function countNodes($nodeName){
        if(!$nodeName){
            throw new Exception(‘Invalid node name.’);
        }
        $nodeCounter=0;
        foreach($this->xml as $node){
            $nodeCounter++;
        }
        return $nodeCounter;
    }
}

If you take a close look at the “XMLParser” class that I listed above, you’ll see that the logic it implements is pretty straightforward. As I said before, the constructor first accepts as an argument the name of the XML file to be parsed, and then verifies whether this file is valid. Once the contents of this file have been loaded by the “simplexml_load_file()” function, they’re assigned to the $this->xml class member.

The remaining methods exposed by the class speak for themselves, so I’ll provide a brief explanation of what they do. That said, the “fetchNodes()” method is tasked with returning an array containing all the nodes that match a given name, while the “fetchAllNodes()” method is responsible for returning all the nodes contained in the given XML file. Regarding this last method, the complete set of nodes will be returned as an array of objects to be processed from outside the class.

Finally, the “countNodes()” method, as its name suggests, returns the number of nodes within the XML file that match a specific name, which can be quite useful if you want to find out how many nodes of a particular type are contained inside an XML file.

Now that you have a clear idea of the tasks performed by each method, take a look at the following script, which shows you precisely how to use them:

try{
    // instantiate new ‘XMLParser’ object
    $xmlPar=new XMLParser(‘users.xml’);
    // fetch <name> nodes
    $nameNodes=$xmlPar->fetchNodes(‘name’);
    // display <name> nodes
    foreach($nameNodes as $name){
        echo ‘Name. ‘.$name.’<br />’;
    }
    // fetch nodes as array of objects
    $nodes=$xmlPar->fetchNodesAsObjects();
    // display nodes
    foreach($nodes as $node){
        echo ‘Postal address: ‘.$node->address.’<br />’;
    }
    // display number of nodes
    echo ‘Found ‘.$xmlPar->countNodes(‘address’).’ address
nodes’;
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

The previous example shows how to utilize the “XMLParser” class, in conjunction with the “users.xml” XML file that I created before. The script calls the “fetchNodes()” method, in order to retrieve all the <name> nodes, which are then displayed by a “foreach” loop.

Next, the “fetchAllNodes()” method is invoked, which comes in handy for returning all the file nodes as an array of objects. Finally, the script displays the number of <address> nodes included within the sample XML file. To complete the example, here’s the output generated by the prior script:

Name. John Doe
Name. Janet Smith
Name. James Smith
Name. Silvia Wilson
Name. Alejandro Gervasio
Postal address: Binary Avenue 1234 FL
Postal address: Crazy Bits Road 4568 CA
Postal address: Socket Boulevard 7894 OH
Postal address: Protocol Avenue 5652 NY
Postal address: Boulevard of Objects 10101 AR
Found 5 address nodes

As you can see, using the “XMLParser” class is a snap, since the “simplexml” extension is remarkably capable of simplifying the parsing of basic XML files. As I expressed right at the beginning of this article, if your application is going to parse simple XML files, this extension might fit your needs perfectly.

Final thoughts

In this first article of the series, I explained the basics of using the “simplexml” extension that comes with PHP 5. As you saw by the different examples, this library is primarily focused on parsing simple XML files, and certainly exposes a good package of functions for doing a decent job.

Since in this tutorial I used only the “simplexml_load_file()” function, in the next one I’ll show you how to utilize other handy functions included in the library, which are useful for searching and replacing nodes. However, you’ll have to wait for the next part. See you there!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan