<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.xml.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.xml-parse.php',
    1 => 'xml_parse',
    2 => 'Start parsing an XML document',
  ),
  'up' => 
  array (
    0 => 'ref.xml.php',
    1 => 'XML Parser Functions',
  ),
  'prev' => 
  array (
    0 => 'function.xml-get-error-code.php',
    1 => 'xml_get_error_code',
  ),
  'next' => 
  array (
    0 => 'function.xml-parse-into-struct.php',
    1 => 'xml_parse_into_struct',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/xml/functions/xml-parse.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.xml-parse" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">xml_parse</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">xml_parse</span> &mdash; <span class="dc-title">Start parsing an XML document</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.xml-parse-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>xml_parse</strong></span>(<span class="methodparam"><span class="type"><a href="class.xmlparser.php" class="type XMLParser">XMLParser</a></span> <code class="parameter">$parser</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$data</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$is_final</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>xml_parse()</strong></span> parses an XML document. The handlers for
   the configured events are called as many times as necessary.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.xml-parse-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">parser</code></dt>
     <dd>
      <p class="para">
       A reference to the XML parser to use.
      </p>
     </dd>
    
    
     <dt><code class="parameter">data</code></dt>
     <dd>
      <p class="para">
       Chunk of data to parse.  A document may be parsed piece-wise by
       calling <span class="function"><strong>xml_parse()</strong></span> several times with new data,
       as long as the <code class="parameter">is_final</code> parameter is set and
       <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> when the last data is parsed.
      </p>
     </dd>
    
    
     <dt><code class="parameter">is_final</code></dt>
     <dd>
      <p class="para">
       If set and <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, <code class="parameter">data</code> is the last piece of
       data sent in this parse.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.xml-parse-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns 1 on success or 0 on failure.
  </p>
  <p class="para">
   For unsuccessful parses, error information can be retrieved with
   <span class="function"><a href="function.xml-get-error-code.php" class="function">xml_get_error_code()</a></span>,
   <span class="function"><a href="function.xml-error-string.php" class="function">xml_error_string()</a></span>,
   <span class="function"><a href="function.xml-get-current-line-number.php" class="function">xml_get_current_line_number()</a></span>,
   <span class="function"><a href="function.xml-get-current-column-number.php" class="function">xml_get_current_column_number()</a></span> and
   <span class="function"><a href="function.xml-get-current-byte-index.php" class="function">xml_get_current_byte_index()</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Some errors (such as entity errors) are reported at the end of the data, thus only if
    <code class="parameter">is_final</code> is set and <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.xml-parse-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.0.0</td>
 <td>
  <code class="parameter">parser</code> expects an <span class="classname"><a href="class.xmlparser.php" class="classname">XMLParser</a></span>
  instance now; previously, a valid <code class="literal">xml</code> <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was expected.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.xml-parse-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="xml_parse.example.chunked">
   <p><strong>Example #1 Chunked parsing of large XML documents</strong></p>
   <div class="example-contents"><p>
     This example shows how large XML documents can be read and parsed in
     chunks, so that it not necessary to keep the whole document in memory.
     Error handling is omitted for brevity.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$stream </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'examples/book-simple.xml'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$parser </span><span style="color: #007700">= </span><span style="color: #0000BB">xml_parser_create</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">xml_set_element_handler</span><span style="color: #007700">(<br />    </span><span style="color: #0000BB">$parser</span><span style="color: #007700">,<br />    function(</span><span style="color: #0000BB">$parser</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$attributes</span><span style="color: #007700">) { echo </span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; },<br />    function(</span><span style="color: #0000BB">$parser</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">) { echo </span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; }<br />);<br /><br />while ((</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">, </span><span style="color: #0000BB">16384</span><span style="color: #007700">))) {<br />    </span><span style="color: #0000BB">xml_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$parser</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">); </span><span style="color: #FF8000">// parse the current chunk<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">xml_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$parser</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">); </span><span style="color: #FF8000">// finalize parsing<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$stream</span><span style="color: #007700">);</span></span></code></div>
   </div>

  </div>
 </div>


</div><?php manual_footer($setup); ?>