

PHP | xml_set_character_data_handler() Function
source link: https://www.geeksforgeeks.org/php-xml_set_character_data_handler-function/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

- Last Updated : 31 Oct, 2019
The xml_set_character_data_handler() function is an inbuilt function in PHP which is used to set the character data handler function for XML parser.
Syntax:
bool xml_set_character_data_handler( resource $xml_parser, callable $data_handler )
Parameters: This function accepts two parameters as mentioned above and described below:
- $xml_parser: It is required parameter. It holds the reference of XML parser to set up character data handler.
- $data_handler: It is required parameter. It is a string which contains the name of function.
handler( resource $parser, string $data )
The handler function must have these two parameters:
- $xml_parser: It holds the reference of XML parser which is calling to handler.
- $data: It holds the character data as a string.
Return Value: This function returns True on success or False on failure.
Note:
- This function is available for PHP 4.0.0 and newer version.
- These examples may not work on online IDE. So, try to run it on local server or php hosted servers.
gfg.xml file:
filter_none
edit
close
play_arrow
link
brightness_4
code
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
user
>
<
username
> user123 </
username
>
<
name
> firstname lastname </
name
>
<
phone
> +91-9876543210 </
phone
>
<
detail
> I am John Doe. Live in Kolkata, India. </
detail
>
</
user
>
Program 1:
filter_none
edit
close
play_arrow
link
brightness_4
code
<?php
// Create an XML parser
$xml_parser
= xml_parser_create();
// Set the character handler function for XML parser
xml_set_character_data_handler(
$xml_parser
,
"char_print"
);
// Opening xml file
$file_pointer
=
fopen
(
"gfg.xml"
,
"r"
);
// Reading xml data from file
while
(
$data
=
fread
(
$file_pointer
, 4096)) {
// Parsing XML data
xml_parse(
$xml_parser
,
$data
,
feof
(
$file_pointer
))
or
// Display error when parsing error occurs
die
(sprintf(
"XML Error: %s at line %d"
,
// Error string
xml_error_string(xml_get_error_code(
$xml_parser
)),
// Current line
xml_get_current_line_number(
$xml_parser
)));
}
// Free to xml parser
xml_parser_free(
$xml_parser
);
fclose(
$file_pointer
);
// Character handler function for XML parser
function
char_print(
$xml_parser
,
$data_to_print
) {
echo
$data_to_print
;
}
?>
Output:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India.
Program 2:
filter_none
edit
close
play_arrow
link
brightness_4
code
<?php
// Create an xml parser
$xml_parser
= xml_parser_create();
// Element handler function named "starting_handler"
// enables custom manupulation for output
function
starting_handler(
$xml_parser
,
$element_name
,
$element_attrs
) {
switch
(
$element_name
) {
case
"USER"
:
echo
"<u>USER DATA</u><br>"
;
break
;
case
"USERNAME"
:
echo
"Username: "
;
break
;
case
"NAME"
:
echo
"Name: "
;
break
;
case
"PHONE"
:
echo
"Phone no: "
;
break
;
case
"DETAIL"
:
echo
"More about user: "
;
}
}
// Element handler function named "ending_handler"
function
ending_handler(
$xml_parser
,
$element_name
) {
echo
"<br>"
;
}
// Character handler function named "char_handler"
function
char_handler(
$xml_parser
,
$data
) {
echo
$data
;
}
// Setting element handlers
xml_set_element_handler(
$xml_parser
,
"starting_handler"
,
"ending_handler"
);
// Setting character data handler
xml_set_character_data_handler(
$xml_parser
,
"char_handler"
);
// Opening xml file
$file_pointer
=
fopen
(
"gfg.xml"
,
"r"
);
// Reading xml file
while
(
$data
=
fread
(
$file_pointer
, 4096)) {
xml_parse(
$xml_parser
,
$data
,
feof
(
$file_pointer
))
or
// Display error while xml parsing
die
(sprintf(
"XML Error: %s at line %d"
,
// Error string
xml_error_string(xml_get_error_code(
$xml_parser
)),
// Error line number
xml_get_current_line_number(
$xml_parser
)));
}
// Free to xml parser
xml_parser_free(
$xml_parser
);
// Closing file stream
fclose(
$file_pointer
);
?>
Output:
USER DATA Username: user123 Name: firstname lastname Phone no: +91-9876543210 More about user: I am John Doe. Live in Kolkata, India.
Reference: https://www.php.net/manual/en/function.xml-set-character-data-handler.php
Recommend
-
11
xml_parser_create_ns() Function Related Articles ...
-
7
PHP proxy to get XML: problem with URL arguments advertisements So I need to retrieve XML from a public API, but my app is in Flash and the public...
-
4
MySQL import: JSON with binary character set Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. ...
-
7
Defining a character set for a column For Oracle database tables advertisements I am running following query in SQL*Plus
-
12
[JavaScript] Scope (Context) of Event Handler Function Updated: March 07, 2015...
-
9
php parse soap xml response 最近公司的一个需求,和外部对接需要用到xml。说明:XML本身不算复杂,但是,加上DTD、XSD、XPath、XSLT等一大堆复杂的规范以后,任何正常的软件开发人员碰到XML都...
-
75
g_49's blog Augnito Coding Test Problem...
-
13
How to convert php array to simple xml 38223 views 1 year ago PHP In this article, i will share with you how to convert...
-
7
How MySQL Uses character_set Configurations ...
-
3
Writing an HTTP handler function in GoCheikh SeckJanuary 17, 2023Introductio...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK