(sigil sxml reader)
(sigil sxml reader) - Streaming XML Parser
A character-by-character streaming XML parser that converts XML to SXML. Designed for protocols like XMPP where XML arrives incrementally over a network connection.
Low-level API (SAX-like)
(define p (make-xml-parser))
(xml-parser-feed! p "<message to=\"user@ex.com\"><body>Hi</body></message>")
(xml-parser-events! p)
; => ((open-tag message ((to "user@ex.com")))
; (open-tag body ())
; (text "Hi")
; (close-tag body)
; (close-tag message))Stanza reader (XMPP-oriented)
(define r (make-stanza-reader))
(stanza-reader-feed! r "<stream:stream xmlns='jabber:client'>")
(stanza-reader-feed! r "<message><body>Hello</body></message>")
(stanza-reader-stanzas! r)
; => ((message (body "Hello")))Convenience
(xml->sxml "<p>Hello</p>") ; => (p "Hello")
(xml->sxml* "<a/><b/>") ; => ((a) (b))Exports
make-xml-parserprocedureCreate a new streaming XML parser.
Returns a parser object that can be fed XML data incrementally.
xml-parser-feed!procedureFeed a string of XML data to the parser.
Events are accumulated and can be retrieved with xml-parser-events!.
xml-parser-events!procedureRetrieve and clear accumulated events.
Returns a list of events, each being one of: (open-tag name attrs) - Opening tag with attribute alist (close-tag name) - Closing tag (text string) - Text content (error message) - Parse error
make-stanza-readerprocedureCreate a new stanza reader for XMPP streams.
The stanza reader wraps a low-level XML parser and accumulates depth-1 elements into complete SXML trees. The root <stream:stream> element is recognized as a stream opener and its attributes are captured separately.
stanza-reader-feed!procedureFeed XML data to the stanza reader.
Parses the data and accumulates complete depth-1 elements as stanzas.
stanza-reader-stanzas!procedureRetrieve and clear completed stanzas.
Returns a list of SXML elements representing complete stanzas.
stanza-reader-stream-attrsprocedureGet the stream attributes from the most recent <stream:stream> opener.
Returns an alist of attributes, or #f if no stream has been opened.
stanza-reader-reset!procedureReset the stanza reader for a new stream.
Called after STARTTLS or SASL authentication when the XML stream is restarted. Clears all parser state but preserves the reader.
xml->sxmlprocedureParse an XML string into a single SXML element.
(xml->sxml "<p class=\"main\">Hello</p>")
; => (p (@ (class "main")) "Hello")xml->sxml*procedureParse an XML string into a list of SXML elements.
(xml->sxml* "<a/><b>text</b>")
; => ((a) (b "text"))