Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP xml_parser_create_ns() Function



The PHP XML Parser xml_parser_create_ns() function is used to returns the resource handle, can be used to create an XML parser with namespace support. A resource handle that can be used by other XML operations is returned. False is returned in the case of failure.

This feature is compatible with PHP versions 4.0.5 and higher. These examples may not be supported by the online IDE. Therefore, try to execute it on a local server or a PHP-hosted server.

Syntax

Below is the syntax of the PHP XML Parser xml_parser_create_ns() function −

resource xml_parser_create_ns ([ string $encoding [, string $separator = ":" ]] )

Parameters

Here are the parameters of the xml_parser_create_ns() function −

  • $encoding: (Optional) It is used to specify the output encoding.

  • $separator: (Optional) It is used to specify the output separator for tag name and namespace.

Return Value

The xml_parser_create_ns() function returns a resource handle that, if successful, will be used by subsequent XML methods. And FALSE on failure.

PHP Version

First introduced in core PHP 4.0.5, the xml_parser_create_ns() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP XML Parser xml_parser_create_ns() function to show how to build a basic XML parser that supports namespaces using default parameters. It only sets up the parser resource.

<?php
   // Create an XML parser
   $p = xml_parser_create_ns();

   // Check if the parser is created successfully
   if (!$p) {
      echo "Failed to create the XML parser with namespace support.";
   } else {
      echo "XML parser with namespace support created successfully.";
   }

   // Free the parser after usage
   xml_parser_free($p);
?>

Output

Here is the outcome of the following code −

XML parser with namespace support created successfully.

Example 2

This application shows how to use the xml_parser_create_ns() function and generates an XML parser with a custom namespace separator (#) to separate tag names and namespaces.

<?php
   // Create an XML parser 
   $parser = xml_parser_create_ns(null, "#");

   // Check if the parser is created successfully
   if (!$parser) {
      echo "Failed to create the XML parser with a custom namespace separator.";
   } else {
      echo "XML parser with a custom namespace separator created successfully.";
   }

   // Free the parser after usage
   xml_parser_free($parser);
?> 

Output

This will create the below output −

XML parser with a custom namespace separator created successfully.

Example 3

This program generates an XML parser using UTF-8 encoding and the xml_parser_create_ns() function. It ensures that when parsing XML data so the parser use a specific character set.

<?php
   // Create an XML parser 
   $p = xml_parser_create_ns("UTF-8");

   // Check if the parser is created successfully
   if (!$p) {
      echo "Failed to create the XML parser with UTF-8 encoding.";
   } else {
      echo "XML parser with UTF-8 encoding created successfully.";
   }

   // Free the parser after usage
   xml_parser_free($p);
?> 

Output

This will generate the below output −

XML parser with UTF-8 encoding created successfully.
php_function_reference.htm
Advertisements