PHP simplexml_load_file() function

Definition and Usage

The simplexml_load_file() function loads an XML document into an object.

If it fails, it returns false.

syntax

simplexml_load_file(file,class,options,ns,is_prefix)
parameters description
file Required. Specifies the XML document to be used.
class Optional. Specifies the class of the new object.
options Optional. Specifies additional Libxml parameters.
ns Optional.
is_prefix Optional.

Return Value

Returns an object of the class SimpleXMLElement, whose properties contain the data in the XML document. If it fails, it returns false.

Example

XML File:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

PHP Code:

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  var_dump($xml);
  }
else
  {
  exit('Error.');
  }
?>

Output:

object(SimpleXMLElement)#1 (4)
{
["to"]=> string(4) "George"
["from"]=> string(4) "John"
["heading"]=> string(8) "Reminder"
["body"]=> string(29) "Don't forget the meeting!"
}