XML Parser Function in PHP: Part 5

Introduction

In this article I describe the PHP XML Parser functions xml_set_end_namespace_decl_handler, xml_set_start_namespace_decl_handler, xml_set_processing_instruction_handler and xml_set_unparsed_entity_decl_handler. To learn some other PHP XML Parser functions go to:

  1.  XML Parser Function in PHP: Part 1
  2.  XML Parser Function in PHP: Part 2
  3.  XML Parser Function in PHP: Part 3
  4. XML Parser Function in PHP: Part  4

PHP xml_set_end_namespace_decl_handler() Function

The PHP XML Parser "xml_set_end_namespace_decl_handler" function is used to set up end namespace declaration handler and this function returns true on success or false on failure.

Syntax

xml_set_end_namespace_decl_handler(parser,handler)

Parameters of the xml_set_end_namespace_decl_handler function

The parameters of the function are:

Parameter Description
parser It specifies the XML parser to use.
handler It specifies a function that must exist when the xml_parser() is called for parser and this "handler" parameter must have two parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. prefix: The prefix is a string used to reference the namespace within an XML object.

PHP xml_set_start_namespace_decl_handler() function

The PHP XML Parser "xml_set_start_namespace_decl_handler" function sets up a start namespace declaration handler and this function returns true on success or false on failure.

Syntax

xml_set_start_namespace_decl_handler(parser,handler)

Parameters of the xml_set_start_namespace_decl_handler function

The parameters of the function are:

Parameter Description
parser It specifies the XML parser to use.
handler It specifies a function that must exist when xml_parser() is called for parser and this "handler" parameter must have three parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. prefix: The prefix is a string used to reference the namespace within an XML object.
  3. uri: It specifies the Uniform Resource Identifier of the namespace.

PHP xml_set_processing_instruction_handler () function

The PHP XML Parser "xml_set_processing_instruction_handler" function sets up a Processing Instruction handler and this function returns true on success or false on failure.

Syntax

xml_set_processing_instruction_handler(parser,handler)

Parameters of the xml_set_processing_instruction_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 when the parser finds a notation declaration and this "handler" parameter must have three parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. target: It specifies a variable containing the Processing Instruction target.
  3. data: It specifies a variable containing the Processing Instruction data.

Example

An example of the function is:


XML File

xml-file.jpg

PHP code

<?php

$parser=xml_parser_create();

function char($parser,$data)

{

echo $data;

}

function pi_handler($parser, $target, $data)

{

echo "Target: $target<br />";

echo "Data: $data<br />";

}

xml_set_character_data_handler($parser,"char");

xml_set_processing_instruction_handler($parser, "pi_handler");

$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

xml-set-processing-instruction--handler-function-in-php.jpg 

PHP xml_set_unparsed_entity_decl_handler() Function

The PHP XML Parser "xml_set_unparsed_entity_decl_handler" function sets up an unparsed entity declaration handler and this function returns true on success or false on failure.

Syntax

xml_set_unparsed_entity_decl_handler(parser,handler)

Parameters of the xml_set_unparsed_entity_decl_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 when the parser finds a notation declaration and this "handler" parameter must have six parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. name: It specifies a variable containing the name of the entity.
  3. base: It specifies a variable containing the base for resolving the system identifier.
  4. system_id: It specifies a variable containing the system identifier for the entity.
  5. public_id: It specifies a variable containing the notation identifying the data type of the entity.
  6. notation: It specifies a variable containing the notation identifying the data type of the entity.

Example

An example of the function is:

 

<html>

<body>

<h3 style="color: red;">xml_set_unparsed_entity_decl_handler() function example in PHP</h3>

 <?php

$xml_parser=xml_parser_create();

function char($xml_parser,$data)

{

echo $data;

}

function unparsed_ent_handler($xml_parser,$entname,$base,$sysID,$pubID,$notname)

{

print "$entname<br />";

print "$sysID<br />";

print "$pubID<br />";

print "$notname<br />";

}

xml_set_character_data_handler($xml_parser,"char");

xml_set_unparsed_entity_decl_handler($xml_parser,"unparsed_ent_handler");

$fp=fopen("test.xml","r");

while ($data=fread($fp,4096))

{

xml_parse($xml_parser,$data,feof($fp)) or

die (sprintf("XML Error: %s at line %d",

xml_error_string(xml_get_error_code($xml_parser)),

xml_get_current_line_number($xml_parser)));

}

xml_parser_free($xml_parser);

?>

</body>

</html>


Output

xml-set-unparsed-entity-decl-handler-function-in-php.jpg


Similar Articles