PHP XML Parser Functions

Beginner
5 min read
Updated: Nov 2026
22 function guides
Function index

About This Index

This page is the PHP XML Parser Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.

Learn how the event-driven parser, handlers, and error reporting fit together on /php/xml-parser. Use this page when you need a specific function such as xml_parser_create(), xml_parse(), or xml_error_string().

Beginner path.

Start with xml_parser_create()—then learn xml_set_element_handler(), xml_set_character_data_handler() and xml_parse(), and finish with xml_get_error_code() and xml_error_string().

PHP 8 uses an XMLParser object.

Since PHP 8.0, xml_parser_create() returns an XMLParser object instead of a resource. xml_parser_free() is a no-op on PHP 8 and is deprecated as of PHP 8.5, because the object is freed for you.

How to Call a PHP XML Parser Function

Every function is global—call it directly. Almost all of them take the parser as the first argument, so create the parser first and pass it along:

PHP
$parser = xml_parser_create();                        // XMLParser

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, 'onStart', 'onEnd');
xml_set_character_data_handler($parser, 'onText');

if (!xml_parse($parser, $xml, true)) {                 // 0 on failure
    $code = xml_get_error_code($parser);               // int
    echo xml_error_string($code);                      // e.g. Mismatched tag
    echo xml_get_current_line_number($parser);         // int, starts at 1
}

$p2 = xml_parser_create();
xml_parse_into_struct($p2, $xml, $values, $index);     // arrays, no handlers

PHP XML Parser Functions Table

All function guides in this PHP tutorial series (22 tutorials in 6 categories).

Create & free

3 functions

Make a parser object, with or without namespace support, and release it when you are done. In PHP 8 the parser is an XMLParser object.

FunctionDescriptionTutorial
xml_parser_create()Create an XML parser and choose the output encoding.Try it
xml_parser_create_ns()Create a namespace-aware parser with a separator character.Try it
xml_parser_free()Legacy free call; no-op on PHP 8, deprecated in PHP 8.5.Try it

Parse

2 functions

Feed XML to the parser. Use handlers for streaming, or read the whole document into arrays.

FunctionDescriptionTutorial
xml_parse()Parse a string or a chunk of XML and call the registered handlers.Try it
xml_parse_into_struct()Parse XML into a flat values array and a tag index.Try it

Options

2 functions

Read and change parser settings such as case folding, target encoding, and white space handling. Set options before you parse.

FunctionDescriptionTutorial
xml_parser_get_option()Get the current value of a parser option.Try it
xml_parser_set_option()Change a parser option, for example turn off case folding.Try it

Errors & position

5 functions

Find out why a parse failed and where the parser is in the document. Lines start at 1; columns and byte indexes start at 0.

FunctionDescriptionTutorial
xml_error_string()Turn an XML error code into a readable message.Try it
xml_get_error_code()Get the error code of the last parse.Try it
xml_get_current_byte_index()Get the byte offset of the parser in the input.Try it
xml_get_current_line_number()Get the line number the parser has reached.Try it
xml_get_current_column_number()Get the column number on the current line.Try it

Core handlers

4 functions

The handlers almost every parser uses: tags, text, a catch-all, and binding handlers to an object.

FunctionDescriptionTutorial
xml_set_element_handler()Set the functions called for opening and closing tags.Try it
xml_set_character_data_handler()Set the function called for text between tags.Try it
xml_set_default_handler()Set the function called for data that has no other handler.Try it
xml_set_object()Use methods of an object as handlers; deprecated in PHP 8.4.Try it

Advanced handlers

6 functions

Handlers for processing instructions, DTD notations and entities, and namespace declarations. Needed only for special documents.

FunctionDescriptionTutorial
xml_set_processing_instruction_handler()Set the function called for processing instructions such as <?php ... ?>.Try it
xml_set_notation_decl_handler()Set the function called for notation declarations in a DTD.Try it
xml_set_unparsed_entity_decl_handler()Set the function called for unparsed entity declarations.Try it
xml_set_external_entity_ref_handler()Set the function called when the document refers to an external entity.Try it
xml_set_start_namespace_decl_handler()Set the function called when a namespace prefix comes into scope.Try it
xml_set_end_namespace_decl_handler()Set the function called when a namespace prefix goes out of scope.Try it

Frequently Asked Questions

They are the xml_* functions of the XML extension, built on the Expat library. You create a parser, register handlers for tags and text, feed it XML with xml_parse(), and read errors and positions when something goes wrong. xml_parse_into_struct() gives you the whole document as arrays instead.
The XML Parser page teaches the ideas: the event-driven model, handlers, error reporting, the struct alternative, namespaces, and the PHP 8 XMLParser object, with three fixed examples. This index lists every function tutorial so you can open xml_parser_create(), xml_parse(), xml_error_string(), and the rest.
Start with xml_parser_create(), then xml_set_element_handler() and xml_set_character_data_handler(), then xml_parse(). After that, learn xml_get_error_code() and xml_error_string() so you can report failures, and try xml_parse_into_struct() when you want arrays.
xml_get_error_code() returns the error number and xml_error_string() turns it into text. xml_get_current_line_number(), xml_get_current_column_number() and xml_get_current_byte_index() tell you where the parser is, both inside a handler and right after a failed xml_parse().
Not in modern PHP. Since PHP 8.0 the parser is an XMLParser object that is freed automatically, so xml_parser_free() does nothing useful and is deprecated as of PHP 8.5. xml_set_object() is deprecated in PHP 8.4; pass callables such as [$object, 'method'] to the handler functions instead.
The XML extension may be disabled in your PHP build. Run var_dump(extension_loaded('xml')); to check. If it prints false, install the XML package for your PHP version (often called php-xml) or build PHP without --disable-xml, then restart PHP.

Start with xml_error_string()

Open the next guide—syntax, examples, and FAQs.

xml_error_string() 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.

6 people found this page helpful