PHP simplexml_load_file()Functions
PHP SimpleXML Reference Manual
Examples
Convert the XML file into a SimpleXMLElement object, then output the keys and elements of the object:
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Run example »
Definition and Usage
The simplexml_load_file() function converts the specified XML file into a SimpleXMLElement object.
Syntax
simplexml_load_file(file,classname,options,ns,is_prefix);
| Parameters | Description |
|---|---|
| file | Required. Specifies the XML file path. |
| classname | Optional. Specifies the class of the new object. |
| options | Optional. Specifies additional Libxml parameters. Set by specifying the option as 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)). Possible values:
|
| ns | Optional. Specifies the namespace prefix or URI. |
| is_prefix | Optional. Specifies a boolean value. Ifnsit is a prefix then TRUE, ifnsit is a URI then FALSE. Default is FALSE. |
Technical Details
| Return value: | Returns a SimpleXMLElement object on success, or FALSE on failure. |
|---|---|
| PHP version: | 5+ |
More Examples
Assume we have the following XML file, "note.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Example 1
Output the data of each element in the XML file:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Run example »
Example 2
Output the element names and data of each child node in the XML file:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
Run example »
PHP SimpleXML Reference Manual Other extensions