XML Parsing in PHP SimpleXML


SimpleXML was added to PHP5 as an easy way to parse xml. I have found some limitations (IE: when the tagname has a colon in it). For those cases, there are other xml parsers to use.

The two main functions for SimpleXML are simplexml_load_string and simplexml_load_file. It loads it into a custom SimpleXML object which can be iterated like an array (for container tags) and cast to a string (for text tags). I have included a basic example below.
<?php
$xml_str='';
$xml_str.="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
$xml_str.="<request>";
$xml_str.=  "<list_title>Must Read Books</list_title>";
$xml_str.=  "<books>";
$xml_str.=     "<book>To Kill a Mockingbird</book>";
$xml_str.=     "<book>The Hobbit</book>";
$xml_str.=     "<book>War and Peace</book>";
$xml_str.=  "</books>";
$xml_str.="</request>";
 
$xml_parsed = simplexml_load_string($xml_str);
 
echo "<hr>";
echo "<div>";
print_r($xml_parsed);
echo "</div>";
echo "<hr>";
echo "<div>";
var_dump($xml_parsed);
echo "</div>";
echo "<hr>";
 
echo "<br>list_title:".$xml_parsed->list_title;
foreach($xml_parsed->books->book as $b)
    echo "<br>book: ".$b;
 
?>

outputs:
SimpleXMLElement Object
(
    [list_title] => Must Read Books
    [books] => SimpleXMLElement Object
        (
            [book] => Array
                (
                    [0] => To Kill a Mockingbird
                    [1] => The Hobbit
                    [2] => War and Peace
                )
 
        )
 
)
--------------------
object(SimpleXMLElement)#1 (2) {
  ["list_title"]=>
  string(15) "Must Read Books"
  ["books"]=>
  object(SimpleXMLElement)#2 (1) {
    ["book"]=>
    array(3) {
      [0]=>
      string(21) "To Kill a Mockingbird"
      [1]=>
      string(10) "The Hobbit"
      [2]=>
      string(13) "War and Peace"
    }
  }
}
 
--------------------
 
list_title:Must Read Books
book: To Kill a Mockingbird
book: The Hobbit
book: War and Peace



Note:
To access individual elements you can do $xml_parsed->books->book[0] and $xml_parsed->books->book[1].

Also Note:
The document tag (in this case 'request') is totally absorbed into the data structure, do you never need to do anything like echo $xml_parsed->request->list_title;
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)