note 15009 deleted from function.xml-set-character-data-handler by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 23:45:30 +0000 |
| Subject: | note 15009 deleted from function.xml-set-character-data-handler by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22372@lists.php.net to get a copy of this message | ||
Like mentioned on ref.xml.php, the cdata-handler is called if there are whitespaces/breaks between
the tags, too.
Example:
<root>
<datahere>foo</datahere>
<datahere>bar</datahere>
</root>
For the first datahere-tag the cdata-handler is called 3 times: the first time with the real data
("foo"), and then two times because of the whitespaces.
It's your parser's turn to differentiate between an empty tag and a whitespace outside of
a tag.
My way:
$lasttag = "";
$handled = false;
function starttag($parser, $tag, $attr)
{
global $lasttag;
global $handled;
$lasttag = $tag;
$handled = false;
}
function cdatahandler($parser, $data)
{
global $lasttag;
global $handled;
if (!$handled and $lasttag == "DATAHERE") {
do_whatever_you_want($data);
$handled = true;
}
}
[Sorry for the ugly code, but the note-system kills all whitespaces. isn't allowed,
too. :( ]
Now the cdata-handler 'knows' where the data came from (i.e. from which tag) and if it
belongs to the tag or not (whitespaces are only found _after_ the tag).
What do you think about this one?
Regards, Till.