note 24011 added to function.xml-set-object
| From: | chiefgeek@php.net | Date: | Sun, 04 Aug 2002 00:25:13 +0000 |
| Subject: | note 24011 added to function.xml-set-object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34245@lists.php.net to get a copy of this message | ||
xml_set_object is great, if you want to hard code your start_element, end_element, etc functions
within your xml class.
But what if you want to increase the modularity of your application?
Solution, create one generic XMLParser class that handles everything *except* the callback funtions.
Then create an abstract XMLCallbackHandler class which you can extend to provide any customization
you want.
So how do you tell the php's xml_parser that you want to use this other class to handle the
callback functions.
xml_set_object? This only works if the functions are *within* the object you called this method
from.
The solution lies in the xml_set_element_handler() function.
Take a look at this sample source...
class XMLParser
{
...
function set_callback_handler($callback_handler)
{
// Assign the startElement endElement functions
xml_set_element_handler(
$this->xml_parser,
array($callback_handler, 'start_element'),
array($callback_handler, 'close_Element' ));
/* By passing an array with this ($object, 'function_name') structure
to the 2nd and 3rd parameters. We are able to tell the parser to
look in another object for these callback functions.
*/
// Assign the character data function
xml_set_character_data_handler(
$this->xml_parser,
array($callback_handler, 'character_data'));
}
}
class myCallBackHandler
{
function start_element(...)
{
//code goes here
}
function end_element(...)
{
//code goes here
}
function character_data(...)
{
//code goes here
}
}
Now all thats left to do is use these classes..
$parser = new XMLParser();
$parser->set_callback_handler(new myCallbackHandler());
--
http://www.php.net/manual/en/function.xml-set-object.php
http://master.php.net/manage/user-notes.php?action=edit+24011
http://master.php.net/manage/user-notes.php?action=delete+24011
http://master.php.net/manage/user-notes.php?action=reject+24011