simplexml_load_file
Description
The simplexml_load_file of SimpleXML for PHP interprets an XML file into an object.
Syntax
simplexml_load_file( string $filename, ?string $class_name = SimpleXMLElement::class, int $options = 0, string $namespace_or_prefix = "", bool $is_prefix = false ): SimpleXMLElement|false
Parameters
filename
Path to the XML file
class_name
An object of the specified class. The class should extend the SimpleXMLElement class.
options
Bitwise OR of the libxml option constants.
namespace_or_prefix
Namespace prefix or URI.
is_prefix
true if namespace_or_prefix is a prefix, false if it's a URI; defaults to false.
Return
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or false on failure.
WARNING: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Use the === operator for testing the return value of this function.
Examples
1 · filename
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/xml/namespace.xml"; $return = simplexml_load_file($filename); if($return === false) { die("simplexml_load_file"); } print_r($return); ?>
SimpleXMLElement Object ( [person] => Array ( [0] => SimpleXMLElement Object ( ) [1] => SimpleXMLElement Object ( ) [2] => SimpleXMLElement Object ( ) ) )
2 · class_name
<? class myclass extends SimpleXMLElement { } $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/xml/namespace.xml"; $class_name = "myclass"; $return = simplexml_load_file($filename, $class_name); if($return === false) { die("simplexml_load_file"); } print_r($return); ?>
myclass Object ( [person] => Array ( [0] => myclass Object ( ) [1] => myclass Object ( ) [2] => myclass Object ( ) ) )