PHP xml_parser_set_option() Function
The PHP xml_parser_set_option() function sets an option value from an XML parser.
Syntax
xml_parser_set_option(parser, option, value)
Parameters
parser |
Required. Specify a reference to the XML parser to set an option in. |
option |
Required. Specify the option to set. Possible values are:
|
value |
Required. Specify the option's new value. |
Return Value
Returns false if parser does not refer to a valid parser or if the option could not be set. Otherwise the option is set and true is returned.
Example:
In the example below, an XML parser is created using xml_parser_create() function. Then, the xml_parser_set_option() function is used to set the option's value for this XML parser.
<?php //create an XML parser $parser=xml_parser_create(); //setting the value of XML_OPTION_CASE_FOLDING $retval1 = xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); if($retval1) echo "XML_OPTION_CASE_FOLDING is set successfully.<br>"; else echo "Getting error while setting XML_OPTION_CASE_FOLDING.<br>"; //setting the value of XML_OPTION_SKIP_TAGSTART $retval2 = xml_parser_set_option($parser, XML_OPTION_SKIP_TAGSTART, 1); if($retval2) echo "XML_OPTION_SKIP_TAGSTART is set successfully.<br>"; else echo "Getting error while setting XML_OPTION_SKIP_TAGSTART.<br>"; //setting the value of XML_OPTION_TARGET_ENCODING $retval3 = xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'US-ASCII'); if($retval3) echo "XML_OPTION_TARGET_ENCODING is set successfully.<br>"; else echo "Getting error while setting XML_OPTION_TARGET_ENCODING.<br>"; //free XML parser xml_parser_free($parser); ?>
The output of the above code will be:
XML_OPTION_CASE_FOLDING is set successfully. XML_OPTION_SKIP_TAGSTART is set successfully. XML_OPTION_TARGET_ENCODING is set successfully.
Example:
Consider the example below where the option's value is attempted to set it to an invalid value. In such cases, a runtime error occurs.
<?php //create an XML parser $parser=xml_parser_create(); //setting the value of XML_OPTION_TARGET_ENCODING $retval = xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-16'); if($retval) echo "XML_OPTION_TARGET_ENCODING is set successfully.<br>"; else echo "Getting error while setting XML_OPTION_TARGET_ENCODING.<br>"; //free XML parser xml_parser_free($parser); ?>
The output of the above code will be:
PHP Fatal error: Uncaught ValueError: xml_parser_set_option(): Argument #3 ($value) is not a supported target encoding in Main.php:6 Stack trace: #0 Main.php(6): xml_parser_set_option() #1 {main} thrown in Main.php on line 6
❮ PHP XML Parser Reference