PHP xml_parser_create() Function

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

Definition

In PHP, xml_parser_create() creates a new XML parser and returns it as an XMLParser object.

This is the first step of event-driven XML parsing: create the parser, set the handlers, and call xml_parse(). In PHP 8 the return value is an XMLParser object. Older PHP versions returned a resource.

Syntax

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

PHP
$parser = xml_parser_create();
CallWhat it does
xml_parser_create()Creates a parser with the default settings.
xml_parser_create('UTF-8')Creates a parser and tells it the source data uses UTF-8.
xml_parser_create('ISO-8859-1')Creates a parser for data in the ISO-8859-1 (Latin-1) encoding.

The encoding argument sets the source encoding of the XML. Supported values are ISO-8859-1, UTF-8, and US-ASCII. Pass null, or leave it out, to let the parser detect it from the XML.

Library declarations
PHP
xml_parser_create(?string $encoding = null): XMLParser

Parameters

ParameterDescription
encodingOptional. The source encoding: ISO-8859-1, UTF-8, or US-ASCII. The default is null.

Return Value

Return ValueDescription
XMLParserA parser object that you pass to the other xml_* functions.

PHP Examples

Example 1 — Create a Parser Object

Create a parser and check that PHP 8 gives you an XMLParser object, not a resource.

PHP
<?php
$parser = xml_parser_create();

echo get_debug_type($parser), PHP_EOL;
var_dump($parser instanceof XMLParser);
echo is_resource($parser) ? "resource" : "object", PHP_EOL;

How It Works

  1. xml_parser_create() builds a new parser with default options.
  2. get_debug_type() shows that the type is XMLParser.
  3. instanceof confirms the value is an object of that class.
  4. is_resource() is false in PHP 8, so the last line prints object.

Example 2 — Choose a Source Encoding

Tell the parser the XML is Latin-1. The byte for é is converted to UTF-8 before it reaches your handler.

PHP
<?php
$xml = "<root>caf\xE9</root>";
$text = '';

$parser = xml_parser_create('ISO-8859-1');
xml_set_character_data_handler(
    $parser,
    function ($parser, $data) use (&$text) {
        $text .= $data;
    }
);

xml_parse($parser, $xml, true);
echo $text, PHP_EOL;

How It Works

  1. The string uses \xE9, which is the single Latin-1 byte for é.
  2. xml_parser_create('ISO-8859-1') tells the parser how to read that byte.
  3. The character data handler collects the text it receives.
  4. The text reaches the handler as UTF-8, so it prints as café.

Example 3 — One New Parser per Document

A parser handles one document. Wrap the create, handler, and parse steps in a function and call it for each XML string.

PHP
<?php
function countItems(string $xml): int
{
    $count = 0;

    $parser = xml_parser_create();
    xml_set_element_handler(
        $parser,
        function ($parser, $name) use (&$count) {
            if ($name === 'ITEM') {
                $count++;
            }
        },
        function ($parser, $name) {
        }
    );

    xml_parse($parser, $xml, true);

    return $count;
}

echo "Items: ", countItems('<root><item>Tea</item><item>Milk</item></root>'), PHP_EOL;
echo "Items: ", countItems('<root></root>'), PHP_EOL;

How It Works

  1. Each call of countItems() creates a fresh parser with xml_parser_create().
  2. The start handler adds 1 for every ITEM tag.
  3. xml_parse() runs the whole document and the handler updates $count.
  4. The first document has two items and the second has none.

Frequently Asked Questions

It returns an XMLParser object. Before PHP 8 it returned a resource, but your code usually does not need to change.
Set handlers with functions like xml_set_element_handler() and xml_set_character_data_handler(), and then call xml_parse() with your XML.
ISO-8859-1, UTF-8, and US-ASCII. The value sets the source encoding of the XML data.
No. Create a new parser for each document so the old state does not cause surprises.
xml_parser_create_ns() understands XML namespaces and joins the namespace URI and the tag name with a separator. Use it when your XML has xmlns attributes.

Next: xml_parser_create_ns()

xml_parser_create_ns() creates an XML parser that understands namespaces and returns it as.

xml_parser_create_ns() 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