simplexml_load_file
Convert an XML document to an object.
simplexml_load_file()
function converts an XML document to an object.
Convert the XML file to an object, and output the keys and elements of the object:
<?php $xml = simplexml_load_file ( "note.xml" ) ; print_r ( $xml ) ; ?>
Run the instance
Suppose we have the following XML file " note.xml ":
<?xml version="1.0" encoding="UTF-8"?> < note > < to > George </ to > < from > John </ from > < heading > Reminder </ heading > < body > Don't forget me this weekend! </ body > </ note >
Output data for 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 ; ?>
Run the instance
Output the element name 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>" ; } ?>
Run the instance
simplexml_load_file ( file , class , options , ns , is_prefix )
parameter | describe |
---|---|
file | Required. Specifies the path to the XML file. |
class | Optional. Specifies the class name of the new object. |
options |
Optional. Specify additional Libxml parameters. Set by specifying options and 1 or 0 (TRUE or FALSE, such as LIBXML_NOBLANKS(1)). Possible values include:
|
ns | Optional. Specifies a namespace prefix or URI. |
is_prefix |
Optional. Specifies a boolean value. If ns is a prefix, it is TRUE; if ns is a URI, it is FALSE. The default is FALSE. |