W3docs

xml_parser_create_ns()

The xml_parser_create_ns() function is a PHP built-in function that creates a new XML parser with namespace support. When parsing XML files that contain

The xml_parser_create_ns() function is a PHP built-in function that creates a new XML parser with namespace support. It belongs to the XML Parser extension and is used when parsing XML documents. Unlike SimpleXML, this function works with an event-driven pull API, allowing you to process XML streams efficiently. When namespace support is enabled, the parser automatically resolves namespace prefixes and passes structured element names to your handlers, eliminating the need for manual prefix resolution.

Syntax

The syntax of the xml_parser_create_ns() function is as follows:

Syntax of the xml_parser_create_ns() function in PHP

xml_parser_create_ns(?string $encoding = null, string $namespace_separator = '?')

Where $encoding is the character encoding used by the XML data (e.g., 'UTF-8'), and $namespace_separator is the character used to join namespace URIs with local names in handler callbacks (default is ?). Both parameters are optional.

Usage Examples

Let's look at practical examples of using xml_parser_create_ns() in PHP.

Example 1: Creating an XML Parser

To create a parser, call xml_parser_create_ns(). This returns a parser resource that must be configured with element and character handlers before parsing.

create a new XML parser with namespace support

$parser = xml_parser_create_ns();
// Configure handlers and parse data...

This code creates a new parser with namespace processing enabled. The parser resource is then used with xml_set_element_handler() and xml_parse() to process XML data.

Example 2: Parsing XML Data with Namespace Prefixes

When namespace support is enabled, the XML Parser extension automatically resolves namespace prefixes. Element names passed to handlers are formatted as namespace_uri?local_name (using the default separator). Namespace resolution is handled by the parser itself, not manually.

parse XML data using the XML Parser extension

$parser = xml_parser_create_ns();
$xml_data = '<?xml version="1.0"?><root xmlns:ns="http://example.com"><ns:child>value</ns:child></root>';

xml_set_element_handler($parser, function($parser, $name) {
    echo "Element: $name\n";
}, function($parser, $name) {
    echo "End Element: $name\n";
});

xml_parse($parser, $xml_data);
xml_parser_free($parser);

This code creates a parser, defines handlers to capture element names, and parses the XML string. The element name ns:child is automatically resolved and passed to the handler as http://example.com?child.

Example 3: Customizing the Character Encoding and Separator

By default, xml_parser_create_ns() assumes the XML data uses the system's default encoding. You can specify an encoding and a custom namespace separator to ensure correct character handling and callback formatting.

customize the encoding and separator for proper character handling

$parser = xml_parser_create_ns('UTF-8', '|');
$xml_data = '<?xml version="1.0"?><root xmlns:ns="http://example.com"><ns:child>value</ns:child></root>';

xml_set_element_handler($parser, function($parser, $name) {
    echo "Element: $name\n";
}, null);

xml_parse($parser, $xml_data);
xml_parser_free($parser);

This code creates a parser with UTF-8 encoding and a custom | separator. The parser resource is freed after use to prevent memory leaks.

Practice

Practice

What output will the PHP code provided at the linked URL produce?