XML Parser Function in PHP: Part 4


Introduction

In this article I describe the PHP XML Parser functions xml_set_default_handler, xml_set_element_handler, xml_set_external_entity_ref_handler, xml_set_notation_decl_handler and xml_set_object. 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

PHP xml_set_default_handler() function

The PHP XML Parser "xml_set_default_handler" function is used to set up default handler and this function returns true on success or false on failure.

Syntax
xml_set_deafult_handler(parser,handler)

Parameters of the xml_set_default_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.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. 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

xml-file.jpg

PHP code
 

<?php

$xml_parser=xml_parser_create();

function default1($xml_parser,$data)

{

echo $data;

}

xml_set_default_handler($xml_parser,"default1");

$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);

?>

Output

xml-set-deafult-handler-function-in-php.jpg

PHP xml_set_element_handler() function

The PHP XML Parser "xml_set_element_handler" function is used to set up start and end element handlers and this function returns true on success or false on failure.

Syntax

 

xml_set_element_handler(parser,start,end)

Parameters of the xml_set_element_handler function

The parameters of the function are:
Parameter Description
parser It specifies the XML parser to use.
start It specifies a function to be called at the start of an element and this "start" parameter has three 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 element.
  3. data: It specifies an array containing the element attributes from the XML file as a string.
end It specifies a function to be called at the end of the element and this "end" parameter must have two parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. name: It specifies a variable containing the name of element.

Example

An example of the function is:


<?php

function starting_handler($parser, $elementname, $attributes)

{

print "Starting handler for $elementname <BR />";

}

function ending_handler($parser, $elementname)

{

print "Ending handler for $elementname <BR />";

}

$xmlfile = 'test.xml';

$xmlparser = xml_parser_create();

xml_set_element_handler($xmlparser, "starting_handler", "ending_handler");

 

// open a file and read data from it for parsing

$fp = fopen($xmlfile,'r');

while ($xmldata = fread($fp,1024))

{

if (!xml_parse($xmlparser, $xmldata, feof($fp)))

{

die( print "ERROR: "

. xml_error_string(xml_get_error_code($xmlparser))

. "<BR />"

. "Line: "

. xml_get_current_line_number($xmlparser)

. "<BR />"

. "Column: "

. xml_get_current_column_number($xmlparser)

. "<BR />");

}

}

xml_parser_free($xmlparser);

?>

Output

 xml-set-element-handler-function-in-php.jpg

PHP xml_set_external_entity_ref_handler() function

The PHP XML Parser "xml_set_external_entity_ref_handler" function is used to set up external entity reference handler and this function returns true on success or false on failure.

Syntax

xml_set_external_entity_ref_handler(parser,handler)

Parameters of the xml_set_external_entity_ref_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 called when parser finds an external entity and this "handler" parameter must have five parameters.
  1. Parser: It specifies a variable containing the XML parser calling the handler.
  2. name: It specifies a variable containing the name of external entity.
  3. base: It specifies the base for resolving the system identifier.
  4. system_id: It specifies the system identifier for external entity.
  5. public_id: It specifies the public identifier for external entity.

Example

An example of the function is:

 

<?php

function starting_handler($parser, $elementname, $attributes)

{

print "Starting handler for $elementname <BR />";

}

function ending_handler($parser, $elementname)

{

print "Ending handler for $elementname <BR />";

}

$xmlfile = 'test.xml';

$xmlparser = xml_parser_create();

xml_set_element_handler($xmlparser, "starting_handler", "ending_handler");

 

// open a file and read data from it for parsing

$fp = fopen($xmlfile,'r');

while ($xmldata = fread($fp,1024))

{

if (!xml_parse($xmlparser, $xmldata, feof($fp)))

{

die( print "ERROR: "

. xml_error_string(xml_get_error_code($xmlparser))

. "<BR />"

. "Line: "

. xml_get_current_line_number($xmlparser)

. "<BR />"

. "Column: "

. xml_get_current_column_number($xmlparser)

. "<BR />");

}

}

xml_parser_free($xmlparser);

?>

Output

 xml-set-external-entity-ref-handler-function-in-php.jpg

PHP xml_set_notation_decl_handler () function

The PHP XML Parser "xml_set_notation_decl_handler" function is used to set up a notation declaration handler and this function returns true on success or false on failure.

Syntax

xml_set_notation_decl_handler (parser,handler)

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

Example

An example of the function is:


<?php

$xml_parser=xml_parser_create();

function char($xml_parser,$xml_data)

{

echo $xml_data;

}

function ext_ent_handler($xml_parser,$ent,$base,$sysID,$pubID)

{

echo "$not<br />";

echo "$sysID<br />";

echo "$pubID<BR />";

}

xml_set_character_data_handler($xml_parser,"char");

xml_set_notation_decl_handler($xml_parser, "ext_ent_handler");

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

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

{

xml_parse($xml_parser,$xml_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);

?>

Output


xml-set-notation-decl-handler-function-in-php.jpg
PHP xml_set_object() function

The PHP XML Parser "xml_set__object" function uses a XML Parser within an object and this function return true on success or false on failure.

Syntax

xml_set_object(parser,object)

Parameters of the xml_set_object function
The parameters of the function are:
Parameter Description
parser It specifies the XML parser to use.
handler It specifies the object to set the parser to.

Example

An example of the function is:

 

<?php

class XMLParser

{

var $parser;

function XMLParser()

{

$this->parser = xml_parser_create();

xml_set_object($this->parser, $this);

xml_set_element_handler($this->parser, "tag_start", "tag_end");

xml_set_character_data_handler($this->parser, "cdata");

}

function parse($data)

{

xml_parse($this->parser, $data);

}

function tag_start($parser, $tag, $attributes)

{

var_dump($parser, $tag, $attributes);

}

function cdata($parser, $cdata)

{

var_dump($parser, $cdata);

}

function tag_end($parser, $tag)

{

var_dump($parser, $tag);

}

}

$xml_parser = new XMLParser();

echo "<pre>";

$xml_parser->parse("<A ID='Welcome'>PHP</A>");

?>

Output

xml-set-object-function-in-php.jpg


Similar Articles