The Klacks parser provides an alternative parsing interface, similar in concept to Java's Streaming API for XML (StAX).
It implements a streaming, "pull-based" API. This is different from SAX, which is a "push-based" model.
Klacks is implemented using the same code base as the SAX parser and has the same parsing characteristics (validation, namespace support, entity resolution) while offering a more flexible interface than SAX.
The following example illustrates creation of a klacks source, use of the consume function to read individual events, and shows some of the most common event types.
* (defparameter *source* (cxml:make-source "<example>text</example>")) *SOURCE* * (klacks:consume *source*) :START-DOCUMENT * (klacks:consume *source*) :START-ELEMENT NIL ;namespace URI "example" ;local name "example" ;qualified name * (klacks:consume *source*) :CHARACTERS "text" * (klacks:consume *source*) :END-ELEMENT NIL "example" "example" * (klacks:consume *source*) :END-DOCUMENT * (klacks:consume *source*) NIL
To parse using Klacks, create an XML source first.
Exact behaviour depends on input, which can be one of the following types:
Closing streams: Sources can refer to Lisp streams that need to be closed after parsing. This includes a stream passed explicitly as input, a stream created implicitly for the pathname case, as well as any streams created automatically for external parsed entities referred to by the document.
All these stream get closed automatically if end of file is reached normally. Use klacks:close-source or klacks:with-open-source to ensure that the streams get closed otherwise.
Keyword arguments have the same meaning as with the SAX parser, please refer to the documentation of parse-file for more information:
In addition, the following argument is for types of input other than pathname:
Events are read from the stream using the following functions:
=> :start-document
or => :start-document, version, encoding, standalonep
or => :dtd, name, public-id, system-id
or => :start-element, uri, lname, qname
or => :end-element, uri, lname, qname
or => :characters, data
or => :processing-instruction, target, data
or => :comment, data
or => :end-document, data
or => nil
peek returns the current event's key and main values.
Return the same values peek would, and in addition advance the source forward to the next event.
Like peek, but return only the values, not the key.
If the current event is :start-element or :end-element, return the corresponding value. Else, signal an error.
If the current event is :characters, return the character data value. Else, signal an error.
If the current event is :characters, determine whether the data was specified using a CDATA section in the source document. Else, signal an error.
Call fn for each attribute of the current start tag in turn, and pass the following values as arguments to the function:
Return a list of SAX attribute structures for the current start tag. Only valid for :start-element.