xml_parse()

xml_parse() — Start parsing an XML document.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

xml_parse() parses an XML document. The handlers for the configured events are called as many times as necessary.

Signature

xml_parse(XMLParser $parser, string $data, bool $is_final = false): int

Parameters

ParameterTypeWhat it takes
$parserXMLParserA reference to the XML parser to use.
$datastringChunk of data to parse. A document may be parsed piece-wise by calling xml_parse() several times with new data, as long as the is_final parameter is set and true when the last data is parsed.
$is_finalboolIf set and true, data is the last piece of data sent in this parse.

Return value

Returns 1 on success or 0 on failure. For unsuccessful parses, error information can be retrieved with xml_get_error_code(), xml_error_string(), xml_get_current_line_number(), xml_get_current_column_number() and xml_get_current_byte_index(). Note: Some errors (such as entity errors) are reported at the end of the data, thus only if is_final is set and true. Some errors (such as entity errors) are reported at the end of the data, thus only if is_final is set and true.

Example

A runnable snippet:

$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);

Changelog

VersionChange
8.0.0parser expects an XMLParser instance now; previously, a valid xml resource was expected.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page xml_parse() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.