PHP simplexml_load_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, simplexml_load_file() reads an XML file and returns a SimpleXMLElement object for its root element.

It works like simplexml_load_string(), except that the XML comes from a file. If the file is missing or the XML is not well-formed, the function returns false instead of an object. For learning and testing, many examples use simplexml_load_string() because it needs no file.

Syntax

Call simplexml_load_file() with the values you need. Start with this line.

PHP
$xml = simplexml_load_file('books.xml');
CallWhat it does
simplexml_load_file('books.xml')Reads books.xml and returns its root element, or false on failure.
simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA)Passes libxml options, here merging CDATA sections into normal text.
@simplexml_load_file($file)Hides the warning PHP prints when the file cannot be opened.

Never use simplexml_load_file() with a file name or URL that comes from a visitor. Always check the result with === false before you use it. For XML you already have as text, simplexml_load_string() is the better choice.

Library declarations
PHP
simplexml_load_file(string $filename, ?string $class_name = SimpleXMLElement::class, int $options = 0, string $namespaceOrPrefix = "", bool $isPrefix = false): SimpleXMLElement|false

Parameters

ParameterDescription
filenameRequired. The path to the XML file.
class_nameOptional. A class that extends SimpleXMLElement. The returned object uses this class. The default is SimpleXMLElement.
optionsOptional. libxml option flags such as LIBXML_NOCDATA, combined with the | operator. The default is 0.
namespaceOrPrefixOptional. A namespace prefix or URI that the elements to read belong to. The default is an empty string, which means no namespace.
isPrefixOptional. Set it to true when the previous argument is a prefix instead of a namespace URI. The default is false.

Return Value

Return ValueDescription
SimpleXMLElementThe root element of the file when the XML is loaded.
falseReturned when the file cannot be read or the XML is not well-formed. PHP also raises a warning unless you handle libxml errors.

PHP Examples

Example 1 — Load a Temporary XML File

Write a small XML file into the temp folder, load it, read two values, and delete the file. The output is the same on every run.

PHP
<?php
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-load-file.xml';
file_put_contents($file, '<book><title>PHP Basics</title><price>9.99</price></book>');

$xml = simplexml_load_file($file);

echo 'Title: ', $xml->title, PHP_EOL;
echo 'Price: ', $xml->price, PHP_EOL;

unlink($file);
echo 'Deleted: ', var_export(!file_exists($file), true), PHP_EOL;

How It Works

  1. The file path points into the system temp folder, and file_put_contents() writes the fixed XML into it.
  2. simplexml_load_file() reads the file and returns the root element, which is book.
  3. $xml->title and $xml->price print the text of the child elements.
  4. unlink() removes the temporary file so nothing is left behind.

Example 2 — Missing and Broken Files

The function returns false for a missing file and for broken XML. Check for false before you use the result.

PHP
<?php
libxml_use_internal_errors(true);

$missing = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-no-such-file.xml';
$xml = @simplexml_load_file($missing);
echo 'Missing file: ', var_export($xml, true), PHP_EOL;

$broken = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-broken.xml';
file_put_contents($broken, '<book><title>PHP</book>');
$xml = simplexml_load_file($broken);
echo 'Broken XML: ', var_export($xml, true), PHP_EOL;
echo 'Has errors: ', var_export(count(libxml_get_errors()) > 0, true), PHP_EOL;

libxml_clear_errors();
unlink($broken);

How It Works

  1. libxml_use_internal_errors(true) stores XML problems in memory instead of printing warnings.
  2. The missing file returns false, and the @ operator hides the normal file warning.
  3. The broken file has a title tag that is never closed correctly, so loading returns false.
  4. libxml_get_errors() holds the stored problems, libxml_clear_errors() empties them, and unlink() deletes the temp file.

Example 3 — Loop Over Items from a File

Load a file with several item elements and read each one with foreach, including an attribute.

PHP
<?php
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun-items.xml';
file_put_contents($file, '<list><item id="1">Pen</item><item id="2">Book</item><item id="3">Bag</item></list>');

$xml = simplexml_load_file($file);

echo 'Count: ', count($xml->item), PHP_EOL;
foreach ($xml->item as $item) {
    echo $item['id'], ': ', $item, PHP_EOL;
}

unlink($file);

How It Works

  1. The temp file holds a list element with three item children.
  2. count($xml->item) counts the item elements, which gives 3.
  3. foreach goes through each item, and $item['id'] reads the id attribute.
  4. echo prints the id and the text of each item, then unlink() deletes the file.

Frequently Asked Questions

It reads an XML file and returns a SimpleXMLElement for the root element, so you can read values with $xml->name and attributes with $xml['name'].
simplexml_load_file() reads the XML from a file, while simplexml_load_string() reads it from a string you already have. Both return the same kind of object.
It returns false when the file cannot be read or the XML is not well-formed. Always compare the result with false before you use it.
Call libxml_use_internal_errors(true) before loading. PHP then stores the problems, and you can read them with libxml_get_errors() and clear them with libxml_clear_errors().
No. Never pass a path or URL that a visitor controls, because they could make PHP read files you did not intend. Use a fixed path or check the value against a list of allowed files.

Next: simplexml_load_string()

simplexml_load_string() turns a string that contains XML into a SimpleXMLElement object th.

simplexml_load_string() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful