Posts tagged xml
Parsing XML in PHP5 with SimpleXML
May 16th
PHP5 includes a library for parsing XML data called SimpleXML. This class (and associated classes) is a fantastic way to parse XML data in PHP5. Parsing of XML comes into almost every project i work on, whether it be creating/manipulating RSS feeds, sending data between Flex and PHP, and whatever else the client wants really
Below is the XML data i will be working with in this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <languages> <language year="1999"> <name>ActionScript 3.0</name> <description>ActionScript is a scripting language based on ECMAScript. ActionScript is used primarily for the development of websites and software using the Adobe Flash Player platform (in the form of SWF files embedded into Web pages).</description> </language> <language year="1995"> <name>PHP</name> <description>PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in standalone graphical applications.</description> </language> <language year="1995"> <name>Java</name> <description>Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.</description> </language> <language year="2007"> <name>LOLCODE</name> <description>LOLCODE is an esoteric programming language inspired by the language expressed in examples of the lolcat Internet meme. The language was created in 2007 by Adam Lindsay, researcher at the Computing Department of Lancaster University.</description> </language> </languages> |
Now, say we wanted to parse this XML file using PHP, and extract the name of each of these languages…
1 2 3 4 5 6 | <?php $languages = simplexml_load_file("languages.xml"); foreach($languages as $language) echo $language->name."<br/>"; ?> |
In the above code, one can extrapolate that SimpleXML will go through your XML file and assign the nodes to an array, and in that array it will create an untyped Object with the data assigned to properties of that Object.
Simple enough, right?
Now, what if you wanted to access an attribute of each of these languages (in this scenario – the year attribute of each language)?
That’s also really easy with SimpleXML! You can reference the attributes of each node by using array notation…
1 2 3 4 5 6 | <?php $languages = simplexml_load_file("languages.xml"); foreach($languages as $language) echo $language["year"]."<br/>"; ?> |
Ok. So what if you wanted your returned data to be mapped to a class? I.e. each element in the array that is returned must be strongly typed to a class, rather than the unspecific stdClass Object that’s returned by default… This can also be done with SimpleXML!
Here’s the Language.php class i created for the returned array elements to be typed as (note that it ,must extend the SimpleXMLElement class):
1 2 3 4 5 6 7 8 | <?php class Language extends SimpleXMLElement { public $year; public $name; public $description; } ?> |
…and the code to return each array element as a Language Object:
1 2 3 4 5 6 7 8 9 | <?php require_once("Language.php"); $languages = simplexml_load_file("languages.xml", "Language"); foreach($languages as $language) { print_r($language); } ?> |
If you look at the output, you will see that the year attribute is not assigned to $year property of the Language class, but rather to a @attributes property. However, if you add this function to the Language.php class, the attributes will be assimilated to their respective properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php class Language extends SimpleXMLElement { public $year; public $name; public $description; public function attrToProp() { foreach($this->attributes() as $prop => $val) $this->$prop = (string) $val; } } ?> |
…and run the script again, but adding this line of code:
1 2 3 4 5 6 7 8 9 10 | <?php require_once("Language.php"); $languages = simplexml_load_file("languages.xml", "Language"); foreach($languages as $language) { $language->attrToProp(); // -- add this line print_r($language); } ?> |



