PHP XML Parser Functions

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().
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().
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:
$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 functionsMake a parser object, with or without namespace support, and release it when you are done. In PHP 8 the parser is an XMLParser object.
| Function | Description | Tutorial |
|---|---|---|
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 functionsFeed XML to the parser. Use handlers for streaming, or read the whole document into arrays.
| Function | Description | Tutorial |
|---|---|---|
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 functionsRead and change parser settings such as case folding, target encoding, and white space handling. Set options before you parse.
| Function | Description | Tutorial |
|---|---|---|
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 functionsFind out why a parse failed and where the parser is in the document. Lines start at 1; columns and byte indexes start at 0.
| Function | Description | Tutorial |
|---|---|---|
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 functionsThe handlers almost every parser uses: tags, text, a catch-all, and binding handlers to an object.
| Function | Description | Tutorial |
|---|---|---|
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 functionsHandlers for processing instructions, DTD notations and entities, and namespace declarations. Needed only for special documents.
| Function | Description | Tutorial |
|---|---|---|
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
Start with xml_error_string()
Open the next guide—syntax, examples, and FAQs.
6 people found this page helpful
