PHP SimpleXML Parser
In this page:
$xml = simplexml_load_string($xml_string); // from text
$xml = simplexml_load_file("file.xml"); // from a file
echo $xml->child->grandchild;
Parsing an XML String
simplexml_load_string($xmlText) parses a string of XML text and returns a SimpleXMLElement object representing the document's structure -- each XML tag becomes an object property you can access with the normal -> operator, just like any other PHP object.
Example: Parsing an XML String
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book>PHP Basics</book></catalog>")`
$xml = simplexml_load_string("<catalog><book>PHP Basics</book></catalog>");
// Print `get_class($xml)` to the output
echo get_class($xml);
?>
Login to try C/C++/Java/PHP code in the editor
Loading XML from a File
simplexml_load_file($path) works exactly like simplexml_load_string(), but reads and parses the XML directly from a file path or a URL, saving you from manually reading the file's contents into a string first.
Example: Loading XML from a File
<?php
// Call `file_put_contents("data.xml", "<catalog><book>PHP Basics</book></catalog>")`
file_put_contents("data.xml", "<catalog><book>PHP Basics</book></catalog>");
// Declare `$xml`, set to `simplexml_load_file("data.xml")`
$xml = simplexml_load_file("data.xml");
// Print `$xml->book` to the output
echo $xml->book;
?>
Login to try C/C++/Java/PHP code in the editor
Accessing Nested Elements
Nested XML tags translate directly into nested object properties -- <catalog><book><title>...</title></book></catalog> becomes $catalog->book->title, letting you walk down through the XML's structure using the same -> chaining you would use for any nested PHP object.
Example: Accessing Nested Elements
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book><title>PHP Basics</title></book></catalog>")`
$xml = simplexml_load_string("<catalog><book><title>PHP Basics</title></book></catalog>");
// Print `$xml->book->title` to the output
echo $xml->book->title;
?>
Login to try C/C++/Java/PHP code in the editor
Looping Over Repeated Elements
When an XML document has multiple sibling elements with the same tag name (like several <item> entries inside a <catalog>), SimpleXML treats them as an iterable collection -- looping over $catalog->item with foreach visits each one in turn.
Example: Looping Over Repeated Elements
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><item>A</item><item>B</item></catalog>")`
$xml = simplexml_load_string("<catalog><item>A</item><item>B</item></catalog>");
// Loop over `$xml->item`, binding each item to `$item`
foreach ($xml->item as $item) {
// Print `$item . "\n"` to the output
echo $item . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Converting SimpleXML to an Array
While SimpleXMLElement objects work well for direct property access, converting one to a plain PHP array with json_decode(json_encode($xml), true) is a handy trick when you want to work with the data using ordinary array functions instead of object syntax.
Example: Converting SimpleXML to an Array
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book>PHP Basics</book></catalog>")`
$xml = simplexml_load_string("<catalog><book>PHP Basics</book></catalog>");
// Declare `$array`, set to `json_decode(json_encode($xml), true)`
$array = json_decode(json_encode($xml), true);
// Print a human-readable dump of `$array`
print_r($array);
?>
Login to try C/C++/Java/PHP code in the editor
- Assuming SimpleXML objects behave exactly like plain arrays or objects for every operation -- some things (like isset checks on repeated elements) work a bit differently than expected.
- Forgetting to check that simplexml_load_file() or simplexml_load_string() actually succeeded before trying to read properties from a false result.
- Not casting a SimpleXMLElement value to a string or appropriate type before using it in string comparisons or concatenation, since it is technically its own object type.
- simplexml_load_string($xmlText) parses an XML string into a SimpleXMLElement object.
- simplexml_load_file($path) does the same, loading directly from a file path or URL.
- Nested XML tags become nested object properties, accessed with the normal -> operator.
SimpleXML has been a core PHP extension since PHP 5 and is enabled by default in most standard PHP installations.
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser