PHP SimpleXML Parser
The PHP SimpleXML Parser
The SimpleXML Parser allows us to easily read and manipulate XML data. It is a lightweight parser and use as few resources as possible. The SimpleXML Parser is ideal for small-sized, uncomplicated XML files.
This parser provides an easy way to access elements, attributes and textual content of an XML document.
Tip: The SimpleXML Parser is a part of the PHP core. No installation is required!
PHP SimpleXML Parser - Read XML
You can read XML from a string or from a file, using these functions:
simplexml_load_string()- Reads XML data from a stringsimplexml_load_file()- Reads XML data from a file
SimpleXML Parser - Read From String
The PHP simplexml_load_string() function is used to read XML data from a string.
The following example shows how to read XML data from a string:
Example
<?php
// Create a string variable that contains XML data
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
// Read from string
$xml = simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>
Run example »
The output of the code above will be:
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )
SimpleXML Parser - Read From File
The PHP simplexml_load_file() function is used to read XML data from a file.
Assume we have an XML file called "note.xml", that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The following example shows how to read XML data from a file:
Example
<?php
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
print_r($xml);
?>
Run example »
The output of the code above will be:
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )
Error Handling
Use the PHP libxml library to deal with XML errors when loading an XML document or string.
The libxml_get_errors() function
returns an array of error objects, or an empty array if there are no errors.
The following example tries to load an XML string with errors:
Example
<?php
libxml_use_internal_errors(true);
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<document>
<user>John Doe</wronguser>
<email>john@example.com</wrongemail>
</document>";
$xml = simplexml_load_string($myXMLData);
if ($xml === false) {
echo "Failed loading XML: ";
// iterate over the
errors
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($xml);
}
?>
Run example »
The output of the code above will be:
Failed loading XML:
Opening and ending tag mismatch: user line 3 and wronguser
Opening and ending tag mismatch: email line 4 and wrongemail
Tip: The next chapter shows how to get/retrieve node values from an XML file with SimpleXML!
PHP SimpleXML Parser Reference
For a complete reference of the PHP SimpleXML Parser functions, visit our PHP SimpleXML Parser Reference page.