First steps

The advantage to SimpleXML is that you no longer need to write any complicated code to access your XML - you simply load it, then read in attributes as you would expect to be able to. Consider the following XML file, employees.xml:

<employees>
    <employee>
        <name>Anthony Clarke</name>
        <title>Chief Information Officer</title>
        <age>48</age>
    </employee>

    <employee>
        <name>Laura Pollard</name>
        <title>Chief Executive Officer</title>
        <age>54</age>
    </employee>
</employees>

As you can see, the base element is a list of employees, and it contains several employee elements. Each employee has a name, a title, and an age. Now take a look at this basic SimpleXML script:

<?php
    $employees = simplexml_load_file('employees.xml');
    var_dump($employees);
?>

That script should be self-explanatory, but just to make sure we're all on the same wavelength, the function simplexml_load_file() opens the XML file specified in parameter one, and returns it as a parsed XML element. In line two, we use var_dump() to output the contents of simplexml_load_file() 's return value, in order that you can see how much work SimpleXML does for you.

Here is the output:

object(simplexml_element)#1 (1) {
    ["employee"]=>
    array(2) {
        [0]=>
        object(simplexml_element)#2 (3) {
            ["name"]=>
            string(14) "Anthony Clarke"
            ["title"]=>
            string(25) "Chief Information Officer"
            ["age"]=>
            string(2) "48"
        }

        [1]=>
        object(simplexml_element)#3 (3) {
            ["name"]=>
            string(13) "Laura Pollard"
            ["title"]=>
            string(23) "Chief Executive Officer"
            ["age"]=>
            string(2) "54"
        }
    }
}

From that, you should be able to see that the base element has an array "employee", containing two elements - one for each of the employees in the XML file. Each element in that array is another object, containing the name, the title, and the age of each employee. Put simply, each collection of data is made into an array, and each distinct XML element is made into an object.

Now, consider the following script, using the same XML file:

<?php
    $employees = simplexml_load_file('employees.xml');

    foreach ($employees->employee as $employee) {
        print "{$employee->name} is {$employee->title} at age {$employee->age}\n";
    }
?>

This time the script actually does something useful with the XML content, and iterates through the $employees->employee array. As each employee element is read from the array, its information is printed out - note how easy it is to read information from elements, simply because the XML is all converted to standard PHP variables.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Reading from a string >>

Previous chapter: SimpleXML

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.