PHP xml_parse() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_parse() feeds XML text to a parser and makes it call your handlers as it finds tags and text.

XML parsing in PHP is event driven. You create a parser, set handlers for start tags, end tags, and text, and then call xml_parse(). The parser reads the data from top to bottom and calls your functions along the way.

Syntax

Call xml_parse() with the values you need. Start with this line.

PHP
$result = xml_parse($parser, $xml, true);
CallWhat it does
xml_parse($parser, $xml, true)Parses the whole document in one call and marks it as complete.
xml_parse($parser, $chunk)Parses one chunk of a larger document. The default is_final value is false.
xml_parse($parser, $chunk, true)Sends the last chunk and tells the parser the document has ended.

In PHP 8 the parser is an XMLParser object. The usual flow is: create the parser with xml_parser_create(), set the handlers, then call xml_parse(). The function returns 1 on success and 0 on failure.

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

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().
dataRequired. A string with XML data. It can be the full document or just one chunk.
is_finalOptional. Set it to true on the last chunk. The default is false.

Return Value

Return ValueDescription
int1 when the data was parsed without errors, 0 when there was an error.

PHP Examples

Example 1 — Create, Set Handlers, Parse

This is the standard flow: create a parser, set the handlers, and call xml_parse() with a small XML string.

PHP
<?php
$xml = '<root><item id="1">Tea</item></root>';

$parser = xml_parser_create();

xml_set_element_handler(
    $parser,
    function ($parser, $name, $attrs) {
        echo "Start: ", $name, PHP_EOL;
    },
    function ($parser, $name) {
        echo "End: ", $name, PHP_EOL;
    }
);

xml_set_character_data_handler(
    $parser,
    function ($parser, $data) {
        echo "Text: ", $data, PHP_EOL;
    }
);

$result = xml_parse($parser, $xml, true);
echo "Result: ", $result, PHP_EOL;

How It Works

  1. xml_parser_create() makes the XMLParser object.
  2. The element handler and character data handler describe what to do for tags and text.
  3. xml_parse() reads the XML and calls the handlers in document order.
  4. It returns 1 because the XML is valid. Names are upper case by default.

Example 2 — Invalid XML Returns 0

When the XML is broken, xml_parse() returns 0 instead of throwing. Read the error to find out why.

PHP
<?php
$parser = xml_parser_create();
$result = xml_parse($parser, '<root><item>Tea</root>', true);

echo "Result: ", $result, PHP_EOL;

if ($result === 0) {
    $code = xml_get_error_code($parser);
    echo "Error: ", xml_error_string($code), " (code ", $code, ")", PHP_EOL;
}

How It Works

  1. The closing tag </root> arrives while item is still open.
  2. xml_parse() returns 0 to say the parse failed.
  3. xml_get_error_code() reads the error number from the parser.
  4. xml_error_string() turns the number into the message mismatched tag.

Example 3 — Parse XML in Chunks

Large files are often read in pieces. Call xml_parse() once per chunk and set is_final to true on the last one.

PHP
<?php
$chunks = [
    '<root><item id="1">Tea</item>',
    '<item id="2">Milk</item></root>',
];

$started = 0;
$ended = 0;

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function () use (&$started) {
        $started++;
    },
    function () use (&$ended) {
        $ended++;
    }
);

foreach ($chunks as $i => $chunk) {
    $isFinal = $i === array_key_last($chunks);
    $ok = xml_parse($parser, $chunk, $isFinal);
    echo "Chunk ", $i + 1, ": result=", $ok, ", started=", $started, ", ended=", $ended, PHP_EOL;
}

How It Works

  1. The document is split into two strings that are sent one after the other.
  2. After chunk 1 the parser has seen ROOT and ITEM start, and the first ITEM end.
  3. After chunk 2 the second ITEM and the closing ROOT are done, so all three tags have ended.
  4. Only the last call passes true for is_final.

Frequently Asked Questions

It returns 1 when the data was parsed successfully and 0 when there was an error. Use xml_get_error_code() to find out what went wrong.
First create the parser with xml_parser_create(), then set handlers such as xml_set_element_handler(), and finally call xml_parse() with your XML.
It tells the parser whether the data you passed is the last piece. Set it to true for the last chunk, or for the whole document when you parse it in one call.
Yes. Call xml_parse() many times with small chunks, and pass true as is_final on the last one. The parser remembers where it stopped.
No. PHP 8 changed the parser to an XMLParser object. The code stays the same, because you just pass the value returned by xml_parser_create().

Next: xml_parse_into_struct()

xml_parse_into_struct() parses an XML string and fills two arrays for you, so you do not n.

xml_parse_into_struct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful