PHP simplexml_load_file()Functions

PHP SimpleXML 参考手册PHP SimpleXML Reference Manual

Examples

Convert the XML file into a SimpleXMLElement object, then output the keys and elements of the object:

<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>

Run example »

Definition and Usage

The simplexml_load_file() function converts the specified XML file into 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 the optimization configuration of nodes (can speed up the application)
  • LIBXML_DTDATTR - Set default DTD attributes
  • LIBXML_DTDLOAD - Load additional subset
  • LIBXML_DTDVALID - Validate DTD validity
  • LIBXML_NOBLANKS - Remove blank nodes
  • LIBXML_NOCDATA - Set CDATA as text nodes
  • LIBXML_NOEMPTYTAG - Expand empty tags (e.g. <br/> to <br></br>), only valid in DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Substitute entities
  • LIBXML_NOERROR - Do not show error reports
  • LIBXML_NONET - Stop accessing the network when loading documents
  • 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 hard limits of the parser. This affects things such as the maximum depth of the document 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 - Do not get 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 the namespace prefix or URI.
is_prefix Optional. Specifies a boolean value. Ifnsit is a prefix then TRUE, ifnsit 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":

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Example 1

Output the data of 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 example »

Example 2

Output the element names 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 example »


PHP SimpleXML 参考手册PHP SimpleXML Reference Manual Other extensions