note 64064 added to function.xml-set-element-handler
| From: | AnonymousKoward at osu1 dot php dot net | Date: | Thu, 06 Apr 2006 17:38:32 +0000 |
| Subject: | note 64064 added to function.xml-set-element-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107463@lists.php.net to get a copy of this message | ||
This has been mentioned before, but I just spent several days trying to figure out what was going
on. Folks, if your XML parser is completely in a class, look at the documentation for
xml_set_object(). The documentation above does say you can use functions inside classes as callbacks
for xml_set_element_handler, but it doesn't tell you that if your entire XML parser is inside a
class, then this is the _WRONG_ way to do things.
You should instead call xml_set_object with your parser variable and $this, which will then fix
strange errors that can otherwise crop up and stop you having to pass an array of ( $this,
'tagStart' ) to this function.
e.g.
<?php
class BadParser
{
function BadParser ()
{
$parser = xml_parser_create();
//This is the WRONG WAY to set the functions inside your class for parsing the XML.
xml_set_element_handler ( $parser, array ( $this, 'tagStart' ), array ( $this,
'tagEnd' ) );
xml_set_character_data_handler ( $parser, array ( $this, 'tagContent' ) );
xml_parse ( $parser, $this->XMLData );
}
function tagStart ( $parser, $tagName, $attributes = NULL )
{
$this->tag = $tagName;
}
function tagEnd ( $parser, $tagName )
{
$this->tag = NULL;
}
function tagContent ( $parser, $content )
{
//This WILL NOT work as you intended. $this->tag will do strange, mysterious things, but it
won't be the tag name like you expected.
echo ( "{$this->tag}: $content" );
}
}
?>
Instead, you should change your constructor to do this as XML initalization instead:
<?php
class GoodParser
{
function GoodParser ()
{
$parser = xml_parser_create();
//This is the RIGHT WAY to set everything inside the object.
xml_set_object ( $parser, $this );
xml_set_element_handler ( $parser, 'tagStart', 'tagEnd' );
xml_set_character_data_handler ( $parser, 'tagContent' );
xml_parse ( $parser, $this->XMLData );
}
/* ... */
}
?>
I don't know if this problem exists in other versions of PHP. My version is 4.4.1. Hope I made
sense, if this note had been around, it would've saved a lot of headaches for me (maybe
I'm not observant enough).
----
Server IP: 64.71.164.2
Probable Submitter: 172.201.149.8
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.xml-set-element-handler.php
Edit -- http://master.php.net/note/edit/64064
Del: integrated -- http://master.php.net/note/delete/64064/integrated
Del: useless -- http://master.php.net/note/delete/64064/useless
Del: bad code -- http://master.php.net/note/delete/64064/bad+code
Del: spam -- http://master.php.net/note/delete/64064/spam
Del: non-english -- http://master.php.net/note/delete/64064/non-english
Del: in docs -- http://master.php.net/note/delete/64064/in+docs
Del: other reasons-- http://master.php.net/note/delete/64064
Reject -- http://master.php.net/note/reject/64064
Search -- http://master.php.net/manage/user-notes.php