PHP xml_parser_set_option() Function



The PHP XML Parser xml_parser_set_option() function is used to set specific XML parser settings. It makes it easier to modify how the parser handles XML data, like changing the text encoding or removing whitespace.

It is necessary to provide the modified value, the change optionand the parser. It returns true if it is successful and false otherwise. As it can result in errors, large documents or invalid options should be handled carefully.

Syntax

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

bool xml_parser_set_option ( XMLParser $xml_parser, int $option, string|int|bool $value )

Parameters

Here are the parameters of the xml_parser_set_option() function −

  • $xml_parser: (Required) It is used to specify the XML parser to use.

  • $option: (Required) It is used to specify the option to set. Possible values are - XML_OPTION_CASE_FOLDING, XML_OPTION_SKIP_TAGSTART, XML_OPTION_SKIP_WHITE and XML_OPTION_TARGET_ENCODING.

  • $value: (Required) It is used to specify options new value.

XML Parser Options

The below options are available:

  • XML_OPTION_CASE_FOLDING (Data type: bool): Controls if the parser converts tag names to uppercase. Enabled by default.

  • XML_OPTION_PARSE_HUGE (Data type: bool): Allows the parser to handle XML documents larger than 10 MB. Use carefully, as it can cause security issues if the file size is not controlled. This option requires libxml2.

  • XML_OPTION_SKIP_TAGSTART (Data type: integer): Specifies how many characters to skip at the start of a tag name.

  • XML_OPTION_SKIP_WHITE (Data type: bool): Decides whether to ignore values that only have spaces or other whitespace characters.

  • XML_OPTION_TARGET_ENCODING (Data type: string): Sets the character encoding for the parsed XML data. Default is the same as the source encoding. Supported options: ISO-8859-1, US-ASCII, and UTF-8.

Return Value

The xml_parser_set_option() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 4, the xml_parser_set_option() 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_set_option() function. The below program shows how to enable or disable case folding in an XML parser. When case folding is enabled, all tags are automatically converted to uppercase. To change this, use XML_OPTION_CASE_FOLDING.

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

   // Disable case folding 
   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);

   // Free the parser
   echo xml_parser_free($parser);
?>

Output

Here is the outcome of the following code −

1

Example 2

Below is the complete code demonstrating the usage of xml_parser_set_option() and XML_OPTION_SKIP_TAGSTART with a working example. In this we are skipping the first few characters of tag names using XML_OPTION_SKIP_TAGSTART and it modifies how the parser reads tags for custom formatting.

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

   // Skip the first 3 characters of tag names
   xml_parser_set_option($parser, XML_OPTION_SKIP_TAGSTART, 3);

   // Define a handler for start tags
   function startElement($parser, $name, $attrs) {
      echo "Start Tag: $name\n";
   }

   // Define a handler for end tags
   function endElement($parser, $name) {
      echo "End Tag: $name\n";
   }

   // Define a handler for character data
   function characterData($parser, $data) {
      echo "Character Data: " . trim($data) . "\n";
   }

   // Set the handlers for the parser
   xml_set_element_handler($parser, "startElement", "endElement");
   xml_set_character_data_handler($parser, "characterData");

   // XML string to parse
   $xmlData = <<<XML
   <abcRoot>
   <abcChild>Value</abcChild>
   </abcRoot>
   XML;

   // Parse the XML string
   if (!xml_parse($parser, $xmlData, true)) {
      die(sprintf(
         "XML error: %s at line %d",
         xml_error_string(xml_get_error_code($parser)),
         xml_get_current_line_number($parser)
      ));
   }

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

Output

This will generate the below output −

Start Tag: ROOT
Character Data: 
Start Tag: CHILD
Character Data: Value
End Tag: CHILD
Character Data: 
End Tag: ROOT
php_function_reference.htm
Advertisements