Current Location: Home> Latest Articles> Cooperate with xml_set_start_namespace_decl_handler, use xml_set_end_namespace_decl_handler to complete the namespace declaration

Cooperate with xml_set_start_namespace_decl_handler, use xml_set_end_namespace_decl_handler to complete the namespace declaration

gitbox 2025-05-26

Below is a PHP sample code using the xml_set_start_namespace_decl_handler and xml_set_end_namespace_decl_handler functions. This code demonstrates how to capture a declaration of a namespace during XML parsing.

 <?php

// create XML Parser
$parser = xml_parser_create();

// Set the callback function at the beginning of the namespace declaration
xml_set_start_namespace_decl_handler($parser, "startNamespaceHandler");

// Set the callback function at the end of the namespace declaration
xml_set_end_namespace_decl_handler($parser, "endNamespaceHandler");

// XML String
$xml_data = <<<XML
<root xmlns:foo="http://gitbox.net/foo" xmlns:bar="http://gitbox.net/bar">
    <foo:item>Item 1</foo:item>
    <bar:item>Item 2</bar:item>
</root>
XML;

// Analysis XML data
if (!xml_parse($parser, $xml_data)) {
    die(sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}

// 释放Parser
xml_parser_free($parser);

// Callback function at the beginning of the namespace declaration
function startNamespaceHandler($parser, $prefix, $uri) {
    echo "Namespace start: Prefix = {$prefix}, URI = {$uri}\n";
}

// Callback function at the end of the namespace declaration
function endNamespaceHandler($parser, $prefix) {
    echo "Namespace end: Prefix = {$prefix}\n";
}

?>

Code parsing

  1. Create an XML parser :
    Use xml_parser_create() to create a new XML parser instance.

  2. Set the namespace declaration callback function :
    The callback functions are set for the start and end stages of the namespace declaration by the xml_set_start_namespace_decl_handler and xml_set_end_namespace_decl_handler functions respectively. These callback functions are automatically called when the parser encounters a namespace declaration.

  3. Parsing XML data :
    Pass the XML data to the parser through the xml_parse() function to start the parsing process. If an error is encountered, xml_error_string() and xml_get_current_line_number() can help us locate and report errors.

  4. Namespace processing :

    • The startNamespaceHandler callback function receives three parameters: the parser instance, the prefix of the namespace, and the URL. When a namespace declaration is encountered, it outputs the prefix and URL of the namespace.

    • The endNamespaceHandler callback function receives two parameters: the prefix of the parser instance and the namespace. It will be called at the end of the namespace declaration, outputting the namespace's prefix.

Things to note

  • In practical applications, XML documents may contain multiple namespace declarations, so you need to adjust the processing logic as needed to ensure that all namespaces are processed correctly.

  • These two callback functions provide a powerful mechanism that allows you to precisely control the parsing process of the namespace, especially useful when dealing with complex XML data.

By combining xml_set_start_namespace_decl_handler and xml_set_end_namespace_decl_handler , you can better manage namespaces when parsing XML documents, ensuring that namespace declarations are handled accurately.