note 14394 deleted from function.xml-set-object by didou
| From: | didou@php.net | Date: | Mon, 01 Dec 2003 15:57:19 +0000 |
| Subject: | note 14394 deleted from function.xml-set-object by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-61212@lists.php.net to get a copy of this message | ||
Note Submitter: jon9mm@hotmail.(xxxxRRRR.)com
----
I have noticed some strange behavior with using the xml parser in a class, and reading xml from a
file, placing the contents in a property, then parsing the string. The reason that I am doing this
is that I want to have a class that can handle either parsing a string or a file, and I figured that
if i could read the file and save its contents to a property, then I could have one generic parse()
function. When I parse from a hardcoded string it works fine, but when I read the contents of a
file then try to parse I get the "Warning: Unable to call handler startElement() in
/home/eprodigy/www/index.php on line 365" error. Any one else had any experience with this??
Here is the class that contains the question in action, sorry for the poor indention, but when I
pasted the tabs got taken out.
class xmlParser {
// properties
var $_objParser;
var $_strXml;
// event handlers
function xmlParser() {
/*
This function will serve as the constructor for the xml parsing class, and
will set properties with default variables, and execute an immediate code
for the class
author Jon Herron
*/
// this function will create the xml parser if it doesnt exist
$this->checkParser();
}
function deconstructor() {
/*
This function will serve as the deconstructor for the xmlParser class and
will unset any vars and execute any code that needs to be run.
author Jon Herron
*/
unset($this->_objParser);
}
// methods
function _parse() {
if(!xml_parse($this->_objParser, $this->_strXml, true)) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($this->_objParser)),
xml_get_current_line_number($this->_objParser)));
}
}
function checkParser($blnCreate = true) {
/*
This function will check to see if the xml parser has been created, and will
return true or false to tell if it has. If false and $blnCreate is true, then
the parser will be created and true returned.
author Jon Herron
*/
$arrClassVars = get_class_vars(get_class($this));
$blnReturn = false;
$strParserVarName = "_objParser";
if($arrClassVars[$strParserVarName] != "") {
$blnReturn = true;
} else {
if($blnCreate) {
$this->_objParser = xml_parser_create();
xml_set_object($this->_objParser, &$this);
xml_set_element_handler($this->_objParser, "startElement",
"endElement");
xml_set_character_data_handler($this->_objParser, "elementData");
$blnReturn = true;
}
}
return($blnReturn);
}
function elementData($parser, $data) {
echo($data);
}
function endElement($parser, $name) {
echo("<hr width=\"100%\">");
}
function startElement($parser, $name, $attrs='') {
}
function parseFromFile($strFullFilePath) {
$filePointer = fopen($strFullFilePath, "r");
$strXml = fread($filePointer, 40096);
fclose($filePointer);
$this->_strXml = chop($strXml);
$this->_parse();
}
function parseFromString($strXml) {
$this->_strXml = $strXml;
$this->_parse();
}
}