How to Access XML Attributes in PHP Using SimpleXML

A user on Programming Talk had a problem the other day. He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).

His parser wasn’t working - because all of the information was stored in attributes instead of child elements. What’s the difference between the two? And how can we fix it?

Use SimpleXML.

Difference Between a Child and an Attribute

For starters, let’s take a look at a sample XML file. This will help illustrate the difference between a child element and an attribute, and it will give us something to work with later on.

<?xml version="1.0" encoding="UTF-8"?>
 
<library>
  <book lang="en">
    <author>John Steinback</author>
    <title>Of Mice and Men</title>
  </book>
 
  <book lang="en">
    <author>F. Scott Fitzgerald</author>
    <title>The Great Gatsby</title>
  </book>  
</library>

This simple XML file is a “library.” It contains a list of books - each of which has an author, a title, and a “lang” attribute.

In this example, “book” is an element. It is a child element of library - which means it is an element created inside the open library element. You could also refer to the library element as the parent of all the book elements.

To create a child element, you simply create a set of tags within a larger set of tags. The book is a child of library, while the author is a child of book. We could create more children for “Author” - like age, sex, and hair color.

By contrast, “lang” is an attribute. It is defined inside the opening tag of an element. By declaring lang="en" we are saying that the book has the attribute of being written in English.

It is simply another (perhaps more annoying) way to store information in an XML file. Most basic XML parsers look for tags and content - but the attribute is neither. It’s contained inside a tag, so it needs to be handled specially.

Thankfully, SimpleXML is designed to help us do just that.

Loading the File in SimpleXML

I saved the xml file above in “xmltest.xml.” In my php script, I can load it into a SimpleXML object with this function call.

$xml = simplexml_load_file('xmltest.xml');

This creates a SimpleXML object which is a tree of connected objects. The root object represents the “library” element. It then has an array of children called “book,” and each “book” element has two children element of its own.

Using the SimpleXML object structure, we can access some pieces of our XML document like this.

var_dump($xml->book[0]); //  All of the info in Book 1
echo $xml->book[0]->author; // First book, John Steinbeck
echo $xml->book[1]->title; // Second book, The Great Gatsby
Accessing the Lang Attribute in the Object Tree

The initial question now resurfaces - how do we access an attribute, like the ‘lang’ attribute?

All of the elements are nicely arranged in a tree. The parent element has a child element, which has a child element, so on and so forth. We can easily access these as related objects like we did above.

The attributes are exploded into an array and attached to the element that they define. So, for example, we defined a ‘lang’ attribute for our book element. Therefore, the value of lang will reside in an array attached to that specific book element.

We can access it like this.

echo $xml->book[0]['lang']; // en

To find an attribute of an object with SimpleXML, you navigate to the element in question and then access an array indexed by the names of the attributes. In this case, our attribute was named ‘lang’ so we use ‘lang’ as a key in the array to get the value.

For simplicity’s sake, it is usually better to define all of your XML data as child elements. This allows you to easily expand data types in the future, and you won’t have to mix methods for accessing the data.

However, if you’re working with someone else’s XML file, you may need to look up attributes. Now you know how.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , ,

One Comment to “How to Access XML Attributes in PHP Using SimpleXML”

  1. How-To: Parse XML with PHP (SimpleXML) | Ev's Tech Thought of the Day said this on

    […] information is available from the PHP website, and there is a really useful blog here too. Share and Enjoy: These icons link to social bookmarking sites where readers can share and […]

Leave a Reply