Source of: /manual/en/function.simplexml-load-file.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.simplexml.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.simplexml-load-file.php',
1 => 'simplexml_load_file',
),
'up' =>
array (
0 => 'ref.simplexml.php',
1 => 'SimpleXML Functions',
),
'prev' =>
array (
0 => 'function.simplexml-import-dom.php',
1 => 'simplexml_import_dom',
),
'next' =>
array (
0 => 'function.simplexml-load-string.php',
1 => 'simplexml_load_string',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.simplexml-load-file" class="refentry">
<div class="refnamediv">
<h1 class="refname">simplexml_load_file</h1>
<p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">simplexml_load_file</span> — <span class="dc-title">
Interprets an XML file into an object
</span></p>
</div>
<a name="function.simplexml-load-file.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">object</span> <span class="methodname"><b>simplexml_load_file</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$filename</tt></span>
[, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$class_name</tt><span class="initializer"> = "SimpleXMLElement"</span></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$options</tt><span class="initializer"> = 0</span></span>
[, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$ns</tt></span>
[, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$is_prefix</tt><span class="initializer"> = false</span></span>
]]]] )</div>
<p class="para rdfs-comment">
Convert the well-formed XML document in the given file to an object.
</p>
</div>
<a name="function.simplexml-load-file.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">filename</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Path to the XML file
</p>
<blockquote><p><b class="note">Note</b>:
Libxml 2 unescapes the URI, so if you want to pass e.g.
<i>b&c</i> as the URI parameter <i>a</i>,
you have to call
<i>simplexml_load_file(rawurlencode('http://example.com/?a=' .
urlencode('b&c')))</i>. Since PHP 5.1.0 you don't need to do
this because PHP will do it for you.
<br />
</p></blockquote>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">class_name</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
You may use this optional parameter so that
<b>simplexml_load_file()</b> will return an object of
the specified class. That class should extend the
<a href="class.simplexmlelement.php" class="type SimpleXMLElement">SimpleXMLElement</a> class.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">options</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Since PHP 5.1.0 and Libxml 2.6.0, you may also use the
<i><tt class="parameter">options</tt></i>
parameter to specify <a href="libxml.constants.php" class="link">additional Libxml parameters</a>.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">ns</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">is_prefix</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.simplexml-load-file.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns an <a href="language.types.object.php" class="type object">object</a> of class <a href="class.simplexmlelement.php" class="type SimpleXMLElement">SimpleXMLElement</a> with
properties containing the data held within the XML document. On errors, it
will return <b><tt class="constant">FALSE</tt></b>.
</p>
</div>
<a name="function.simplexml-load-file.errors"></a><div class="refsect1 errors">
<h3 class="title">Errors/Exceptions</h3>
<p class="para">
Produces an <b><tt class="constant">E_WARNING</tt></b> error message for each error
found in the XML data.
</p>
<div class="tip"><b class="tip">Tip</b>
<p class="para">
Use <a href="function.libxml-use-internal-errors.php" class="function">libxml_use_internal_errors()</a> to suppress all XML
errors, and <a href="function.libxml-get-errors.php" class="function">libxml_get_errors()</a> to iterate over them
afterwards.
</p>
</div>
</div>
<a name="function.simplexml-load-file.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Interpret an XML document</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// The file test.xml contains an XML document with a root element<br />// and at least an element /[root]/title.<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.xml'</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$xml </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_load_file</span><span style="color: #007700">(</span><span style="color: #DD0000">'test.xml'</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);<br />} else {<br /> exit(</span><span style="color: #DD0000">'Failed to open test.xml.'</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>
This script will display, on success:
</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
SimpleXMLElement Object
(
[title] => Example Title
...
)
</pre></div>
</div>
<div class="example-contents simpara"><p>
At this point, you can go about using <i>$xml->title</i>
and any other elements.
</p></div>
</div><p>
</p>
</div>
<a name="function.simplexml-load-file.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.simplexml-load-string.php" class="function" rel="rdfs-seeAlso">simplexml_load_string()</a> - Interprets a string of XML into an object</li>
<li class="member"> <span class="methodname"><a href="simplexmlelement.construct.php" class="function" rel="rdfs-seeAlso">SimpleXMLElement::__construct</a> - Creates a new SimpleXMLElement object</span></li>
<li class="member"><a href="simplexml.examples-errors.php" class="xref">Dealing with XML errors</a></li>
<li class="member"><a href="function.libxml-use-internal-errors.php" class="function" rel="rdfs-seeAlso">libxml_use_internal_errors()</a> - Disable libxml errors and allow user to fetch error information as needed</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>