Low-level interface to Expat. Unless speed is paramount, this should
normally be avoided in favour of the interfaces provided by
Text.XML.Expat.SAX
and Text.XML.Expat.Tree
. Basic usage is:
- Make a new parser:
newParser
. - Set up callbacks on the parser:
setStartElementHandler
, etc. - Feed data into the parser:
parse
,parse'
orparseChunk
. Some of these functions must be wrapped inwithParser
.
- data Parser
- newParser :: Maybe Encoding -> IO Parser
- parse :: Parser -> ByteString -> IO (Maybe XMLParseError)
- parse' :: Parser -> ByteString -> IO (Maybe XMLParseError)
- withParser :: Parser -> (ParserPtr -> IO a) -> IO a
- type ParserPtr = Ptr Parser_struct
- data Parser_struct
- parseChunk :: ParserPtr -> ByteString -> Bool -> IO (Maybe XMLParseError)
- data Encoding
- data XMLParseError = XMLParseError String XMLParseLocation
- getParseLocation :: ParserPtr -> IO XMLParseLocation
- data XMLParseLocation = XMLParseLocation {}
- type StartElementHandler = ParserPtr -> CString -> [(CString, CString)] -> IO Bool
- type EndElementHandler = ParserPtr -> CString -> IO Bool
- type CharacterDataHandler = ParserPtr -> CStringLen -> IO Bool
- type ExternalEntityRefHandler = Parser -> CString -> CString -> CString -> CString -> IO Bool
- type SkippedEntityHandler = ParserPtr -> CString -> Int -> IO Bool
- setStartElementHandler :: Parser -> StartElementHandler -> IO ()
- setEndElementHandler :: Parser -> EndElementHandler -> IO ()
- setCharacterDataHandler :: Parser -> CharacterDataHandler -> IO ()
- setExternalEntityRefHandler :: Parser -> ExternalEntityRefHandler -> IO ()
- setSkippedEntityHandler :: Parser -> SkippedEntityHandler -> IO ()
- setUseForeignDTD :: Parser -> Bool -> IO ()
- parseExternalEntityReference :: Parser -> CString -> Maybe Encoding -> CStringLen -> IO Bool
- data ExpatHandlers
- encodingToString :: Encoding -> String
Parser Setup
Parsing
parse :: Parser -> ByteString -> IO (Maybe XMLParseError)Source
parse data
feeds lazy ByteString data into a Parser
. It returns
Nothing on success, or Just the parse error.
parse' :: Parser -> ByteString -> IO (Maybe XMLParseError)Source
parse data
feeds strict ByteString data into a Parser
. It returns
Nothing on success, or Just the parse error.
:: Parser | |
-> (ParserPtr -> IO a) | Computation where parseChunk and other low-level functions may be used |
-> IO a |
Most of the low-level functions take a ParserPtr so are required to be
called inside withParser
.
type ParserPtr = Ptr Parser_structSource
data Parser_struct Source
Opaque parser type.
parseChunk :: ParserPtr -> ByteString -> Bool -> IO (Maybe XMLParseError)Source
parseChunk data False
feeds strict ByteString data into a
Parser
. The end of the data is indicated by passing True
for the
final parameter. It returns Nothing on success, or Just the parse error.
data XMLParseError Source
Parse error, consisting of message text and error location
data XMLParseLocation Source
Specifies a location of an event within the input text
XMLParseLocation | |
|
Parser Callbacks
type StartElementHandler = ParserPtr -> CString -> [(CString, CString)] -> IO BoolSource
The type of the "element started" callback. The first parameter is the element name; the second are the (attribute, value) pairs. Return True to continue parsing as normal, or False to terminate the parse.
type EndElementHandler = ParserPtr -> CString -> IO BoolSource
The type of the "element ended" callback. The parameter is the element name. Return True to continue parsing as normal, or False to terminate the parse.
type CharacterDataHandler = ParserPtr -> CStringLen -> IO BoolSource
The type of the "character data" callback. The parameter is the character data processed. This callback may be called more than once while processing a single conceptual block of text. Return True to continue parsing as normal, or False to terminate the parse.
type ExternalEntityRefHandler = Parser -> CString -> CString -> CString -> CString -> IO BoolSource
The type of the "external entity reference" callback. See the expat documentation.
type SkippedEntityHandler = ParserPtr -> CString -> Int -> IO BoolSource
Set a skipped entity handler. This is called in two situations:
- An entity reference is encountered for which no declaration has been read and this is not an error.
- An internal entity reference is read, but not expanded, because
XML_SetDefaultHandler
has been called.
setStartElementHandler :: Parser -> StartElementHandler -> IO ()Source
Attach a StartElementHandler to a Parser.
setEndElementHandler :: Parser -> EndElementHandler -> IO ()Source
Attach an EndElementHandler to a Parser.
setCharacterDataHandler :: Parser -> CharacterDataHandler -> IO ()Source
Attach an CharacterDataHandler to a Parser.
Lower-level interface
data ExpatHandlers Source