SAX handlers
- So far we've listed the ABSTRACT-HANDLER class, but there are various subclasses.
- "Abstract class" means that you'd need to make your own subclass of ABSTRACT-HANDLER, and implement all of its methods. Good for error checking, so that you don't miss any event! Less good for short tutorial examples. So let's use one of the other classes:
- The CONTENT-HANDLER still forces you to implement methods for all events related to normal XML content. (It handles only boring DTD stuff for you; more on that later.)
- The DEFAULT-HANDLER is the convenient one, by default it's like a NIL handler, and you get to define only the event methods that you care about.
SAX:ABSTRACT-HANDLER () | class |
SAX:CONTENT-HANDLER (sax:abstract-handler) | class |
SAX:DEFAULT-HANDLER (sax:content-handler) | class |
This time we collect the characters, too:
CL-USER> (defclass tostring (sax:default-handler) ())
#<STANDARD-CLASS TOSTRING>
CL-USER> (defvar *special*)
*SPECIAL*
CL-USER> (defmethod sax:characters ((handler tostring) data) (write-string data *special*))
#<STANDARD-METHOD SAX:CHARACTERS (TOSTRING T)>
CL-USER> (with-output-to-string (*tostring*) (cxml:parse "<x>111<y>222</y>333</x>" (make-instance 'tostring)))
"111222333"