PHP simplexml_load_file() Function

## PHP Basic

PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

## PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

## PHP Advanced

PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

## PHP Database

PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

## PHP XML

XML Expat Parser XML DOM XML SimpleXML

## PHP and AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

## PHP Reference

PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

## PHP Quiz

PHP Quiz PHP Certificate

# PHP simplexml_load_file() Function

PHP SimpleXML Reference

## Example

Convert an XML file into a SimpleXMLElement object, then output 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);

Parameter | Description
--- | ---
file | Required. Specifies the path to the XML file
classname | Optional. Specifies the class of the new object

options

Optional. Specifies additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1))

Possible values:

- LIBXML_COMPACT - Activate nodes allocation optimization (may speed up application)
- LIBXML_DTDATTR - Set default DTD attributes
- LIBXML_DTDLOAD - Load external subset
- LIBXML_DTDVALID - Validate with the DTD
- LIBXML_NOBLANKS - Remove blank nodes
- LIBXML_NOCDATA - Merge CDATA as text nodes
- LIBXML_NOEMPTYTAG - Expand empty tags (e.g. <br/> to <br></br>), only available in the DOMDocument->save() and DOMDocument->saveXML() functions
- LIBXML_NOENT - Substitute entities
- LIBXML_NOERROR - Do not show error reports
- LIBXML_NONET - Disable network access while 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 - Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document and limits of the size of text nodes
- LIBXML_XINCLUDE - Implement XInclude substitution
- LIBXML_ERR_ERROR - Get recoverable errors
- LIBXML_ERR_FATAL - Get fatal errors
- LIBXML_ERR_NONE - Get no errors
- LIBXML_ERR_WARNING - Get simple warnings
- LIBXML_VERSION - Get libxml version (e.g. 20605 or 20617)
- LIBXML_DOTTED_VERSION - Get dotted libxml version (e.g. 2.6.5 or 2.6.17)

ns | Optional. Specifies a 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. 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 from 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's name and data for 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 Reference

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE