Current Location: Home> Function Categories> simplexml_load_file

simplexml_load_file

Convert an XML document to an object.
Name:simplexml_load_file
Category:Uncategorized
Programming Language:php
One-line Description:Convert an XML document to an object.

Definition and usage

simplexml_load_file() function converts an XML document to an object.

Example

Example 1

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

Example 2

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

Example 3

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

grammar

 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:

  • LIBXML_COMPACT - Activate node allocation optimization (may accelerate application)
  • LIBXML_DTDATTR - Set the default DTD attribute
  • LIBXML_DTDLOAD - Loading external subsets
  • LIBXML_DTDVALID - Verify using DTD
  • LIBXML_NOBLANKS - Remove blank nodes
  • LIBXML_NOCDATA - Merge CDATA into text nodes
  • LIBXML_NOEMPTYTAG - Extended empty tags (e.g. <br/> to <br></br>), available only in DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Replace entity
  • LIBXML_NOERROR - No error report displayed
  • LIBXML_NONET - Disable network access when loading a document
  • LIBXML_NOWARNING - Warning report not displayed
  • LIBXML_NOXMLDECL - Omit XML declaration when saving a document
  • LIBXML_NSCLEAN - Remove redundant namespace declarations
  • LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag to relax the hard-coded limits of the parser. This affects the maximum depth of the document and the limits of the text node size
  • LIBXML_XINCLUDE - Implement XInclude replacement
  • LIBXML_ERR_ERROR - Get recoverable errors
  • LIBXML_ERR_FATAL - Get a fatal error
  • LIBXML_ERR_NONE - Get No Errors
  • LIBXML_ERR_WARNING - Get a simple warning
  • LIBXML_VERSION - Get the libxml version (for example 20605 or 20617)
  • LIBXML_DOTTED_VERSION - Get the dotted version of libxml (for example, 2.6.5 or 2.6.17)
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.

Similar Functions
Popular Articles