PHP.nl

xml_parse

xml_parse

Start parsing an XML document

int **xml_parse** XMLParser $parser string $data bool $is_final
parses an XML document. The handlers for

the configured events are called as many times as necessary. xml_parse

parserA reference to the XML parser to use.

data Chunk of data to parse. A document may be parsed piece-wise by calling several times with new data, as long as the parameter is set and true when the last data is parsed. xml_parse``is_final

is_final If set and true, is the last piece of data sent in this parse. data

Returns 1 on success or 0 on failure.

For unsuccessful parses, error information can be retrieved with , , , and . xml_get_error_code``xml_error_string``xml_get_current_line_number``xml_get_current_column_number``xml_get_current_byte_index

Opmerking: > Some errors (such as entity errors) are reported at the end of the data, thus only if is set and true. is_final

Voorbeeld: Chunked parsing of large XML documents

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.

<?php
$stream = fopen('examples/book-simple.xml', 'r');
$parser = xml_parser_create();

xml_set_element_handler(
    $parser,
    function($parser, $name, $attributes) { echo $name, PHP_EOL; },
    function($parser, $name) { echo $name, PHP_EOL; }
);

while (($data = fread($stream, 16384))) {
    xml_parse($parser, $data); // parse the current chunk
}
xml_parse($parser, '', true); // finalize parsing
fclose($stream);