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:
- XML Parser Function in PHP: Part 1
- XML Parser Function in PHP: Part 2
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
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:
- XML_OPTION_CASE_FOLDING: It specifies if case-folding is enabled.
- XML_OPTION_SKIP_TAGSTART: It specifies how many characters should be skipped in the beginning of a tag name.
|
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:
- XML_OPTION_CASE_FOLDING: It specifies if case-folding is enabled.
- XML_OPTION_SKIP_TAGSTART: It specifies how many characters should be skipped in the beginning of a tag name.
- XML_OPTION_SKIP_WHITE: It specifies whether to skip values consisting of whitespace characters
- XML_OPTION_TARGET_ENCODING: It specifies which target encoding to use in this XML parser.
|
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.
- Parser: It specifies a variable containing the XML parser calling the handler.
- data: It specifies a variable containing the character data from the XML file as a string.
|
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