Introduction
In this article I describe the PHP XML Parser functions xml_parser_create, xml_parser_free, xml_parser_get_option, xml_parser_set_option and xml__set_character_data_handler . To learn some other PHP XML Parser functions go to:
PHP xml_parser_create() Function
The PHP XML Parser "xml_parser_create" function is used to create an XML parser.
Syntax
| xml_parser_create(encoding) |
Parameter of the xml_parser_create function
The parameter of the function is:
| Parameter | Description |
| encoding | It specifies the output encoding and it's possible values are:
|
Example
An example of the function is:
<?php
$xmlparser = xml_parser_create();
xml_parser_free($xmlparser);
?>
PHP xml_parser_free() Function
The PHP XML Parser "xml_parser_free" function frees an XML parser and this function returns FALSE if parser does not refer to a valid parser, or else it frees the parser and returns TRUE.
Syntax
| xml_parser_free(parser) |
Parameter of the xml_parser_free function
The parameter of the function is:
| Parameter | Description |
| parser | It specifies the XML parser to free. |
Example
An example of the function is:
<?php
$xmlparser = xml_parser_create();
xml_parser_free($xmlparser);
?>
PHP xml_parser_get_option() Function
The PHP XML Parser "xml_parser_get_option" function is used to get options from an XML parser and this function returns FALSE if parser does not refer to a valid parser otherwise option's value is returned.
Syntax
| xml_parser_get_option(parser,option,value) |
Parameters of the xml_parser_get_option function
The parameters of the function are:
| Parameter | Description |
| parser | It specifies the XML parser to use. |
| option | It specifies the option to get and it's possible values are:
|
| value | It specifies options new value. |
Example
An example of the function is:
<?php
$xmlparser = xml_parser_create();
xml_parser_get_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_free($xmlparser);
?>
PHP xml_parser_set_option() Function
The PHP XML Parser "xml_parser_set_option" function sets options in an XML parser and this function returns false if parser does not refer to a valid parser otherwise the option is set and true is retuned.
Syntax
| xml_parser_get_option(parser,option,value) |
Parameters of the xml_parser_set_option function
The parameters of the function are:
| Parameter | Description |
| parser | It specifies the XML parser to use. |
| option | It specifies the option to set and it's possible values are:
|
| value | It specifies options new value. |
Example
An example of the function is:
<?php
$xmlparser = xml_parser_create();
xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_free($xmlparser);
?>
PHP xml__set_character_data_handler Function
The PHP XML Parser "xml__set_character_data_handler" function is used to set up a character data handler and this function returns true on success or false on failure.
Syntax
| xml__set_character_data_handler (parser,handler) |
Parameters of the xml__set_character_data_handler function
The parameters of the function are:
| Parameter | Description |
| parser | It specifies the XML parser to use. |
| handler | It specifies a function to be used an an event handler and this "handler" parameter must have two parameters.
|
Example
An example of the function is:
XML File

PHP code
<?php
$Parser=xml_parser_create();
function char($parser,$data)
{
echo $data;
}
xml_set_character_data_handler($Parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096))
{
xml_parse($Parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($Parser)));
}
xml_parser_free($Parser);
?>
Output

Comments
Join the conversation! Your thoughts help the community grow.