PHP simplexml_load_file() Function
PHP SimpleXML Reference Manual
Example
Convert an XML file to a SimpleXMLElement object, then output the object's keys and elements:
Run Example »
Definition and Usage
The simplexml_load_file() function converts the specified XML file to 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:
|
LIBXML_COMPACT - Activate optimized node configuration (can speed up the application)
LIBXML_DTDATTR - Set default DTD attributes
LIBXML_DTDLOAD - Load the external subset
LIBXML_DTDVALID - Validate DTD validity
LIBXML_NOBLANKS - Remove empty nodes
LIBXML_NOCDATA - Set CDATA as text nodes
LIBXML_NOEMPTYTAG - Expand empty tags (e.g.
to
), only valid in the DOMDocument->save() and DOMDocument->saveXML() functions
LIBXML_NOENT - Substitute entities
LIBXML_NOERROR - Do not show error reports
LIBXML_NONET - Stop network access when loading the document
LIBXML_NOWARNING - Do not show warning reports
LIBXML_NOXMLDECL - Drop the XML declaration when saving a document
LIBXML_NSCLEAN - Remove redundant namespace declarations
LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag to relax any mandatory limits of the parser. This affects things such as the maximum document depth and text node size limits.
LIBXML_XINCLUDE - Use XInclude substitution
LIBXML_ERR_ERROR - Get recoverable errors
LIBXML_ERR_FATAL - Get fatal errors
LIBXML_ERR_NONE - Get no errors
LIBXML_ERR_WARNING - Get simple warnings
LIBXML_VERSION - Get the libxml version (e.g. 20605 or 20617)
LIBXML_DOTTED_VERSION - Get the dotted libxml version (e.g. 2.6.5 or 2.6.17)
| | *ns* | Optional. Specifies a namespace prefix or URI. | | *is_prefix* | Optional. Specifies a boolean. If *ns* is a prefix then TRUE, if *ns* 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":
Tove
Jani
Reminder
Don't forget me this weekend!
Example 1
Output the data of each element in the XML file:
to . "
"; echo $xml->from . "
"; echo $xml->heading . "
"; echo $xml->body; ?>
Run Example »
Example 2
Output the element names and data of each child node in the XML file:
getName() . "
"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "
"; } ?>
Run Example »
PHP SimpleXML Reference Manual