Easy Tutorial
❮ Php Var_Export Function Func Mysqli Query ❯

PHP simplexml_load_file() Function

PHP SimpleXML Reference Manual

Example

Convert an XML file to a SimpleXMLElement object and then output the keys and elements of the object:


Definition and Usage

The simplexml_load_file() function converts the specified XML file into a SimpleXMLElement object.


Syntax

Parameter 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 options as 1 or 0 (TRUE or FALSE, e.g., LIBXML_NOBLANKS(1)). Possible values: LIBXML_COMPACT - Activate node optimization (can speed up the application)<br> LIBXML_DTDATTR - Set default DTD attributes<br> LIBXML_DTDLOAD - Load additional subsets<br> LIBXML_DTDVALID - Validate DTD validity<br> LIBXML_NOBLANKS - Remove blank nodes<br> LIBXML_NOCDATA - Set CDATA as text nodes<br> LIBXML_NOEMPTYTAG - Expand empty tags (e.g., <br/> to <br></br>), only valid in DOMDocument->save() and DOMDocument->saveXML() functions<br> LIBXML_NOENT - Substitute entities<br> LIBXML_NOERROR - Do not show error reports<br> LIBXML_NONET - Stop network access when loading documents<br> LIBXML_NOWARNING - Do not show warning reports<br> LIBXML_NOXMLDECL - Discard the XML declaration when saving a document<br> LIBXML_NSCLEAN - Remove redundant namespace declarations<br> LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of the document and text node size, among others.<br> LIBXML_XINCLUDE - Use XInclude substitution<br> LIBXML_ERR_ERROR - Get recoverable errors<br> LIBXML_ERR_FATAL - Get fatal errors<br> LIBXML_ERR_NONE - Get no errors<br> LIBXML_ERR_WARNING - Get simple warnings<br> LIBXML_VERSION - Get the libxml version (e.g., 20605 or 20617)<br> LIBXML_DOTTED_VERSION - Get the dotted libxml version (e.g., 2.6.5 or 2.6.17)
ns Optional. Specifies the namespace prefix or URI.
is_prefix Optional. Specifies a boolean value. TRUE if ns is a prefix, FALSE if ns is a URI. Default is FALSE.

Technical Details

Return Value: Returns a SimpleXMLElement object on success, or FALSE on failure.
PHP Version: 5+
--- ---

More Examples

Suppose we have the following XML file, "note.xml":

Example 1

Output data for each element in the XML file:

Example 2

Output the element name and data for each child node in the XML file:


❮ Php Var_Export Function Func Mysqli Query ❯