|
|
|
|
|
Description |
This module provides functions to parse an XML document to a tree structure,
either strictly or lazily, as well as a lazy SAX-style interface.
The GenericXMLString type class allows you to use any string type. Three
string types are provided for here: String, ByteString and Text.
Error handling in strict parses is very straight forward - just check the
Either return value. Lazy parses are not so simple. Here are two working
examples that illustrate the ways to handle errors. Here they are:
Way no. 1
import Text.XML.Expat.Tree
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Internal (c2w)
import Control.Exception.Extensible as E
-- This is the recommended way to handle errors in lazy parses
main = do
let (tree, mError) = parseTree Nothing (L.pack $ map c2w $ "<top><banana></apple></top>")
print (tree :: UNode String)
-- Note: We check the error _after_ we have finished our processing on the tree.
case mError of
Just err -> putStrLn $ "It failed : "++show err
Nothing -> putStrLn "Success!"
Way no. 2
...
import Control.Exception.Extensible as E
-- This is not the recommended way to handle errors.
main = do
do
let tree = parseTreeThrowing Nothing (L.pack $ map c2w $ "<top><banana></apple></top>")
print (tree :: UNode String)
-- Because of lazy evaluation, you should not process the tree outside the 'do' block,
-- or exceptions could be thrown that won't get caught.
`E.catch` (\exc ->
case E.fromException exc of
Just (XMLParseException err) -> putStrLn $ "It failed : "++show err
Nothing -> E.throwIO exc)
|
|
Synopsis |
|
|
|
|
Tree structure
|
|
|
The tree representation of the XML document.
| Constructors | Element | | eName :: !tag | | eAttrs :: ![(tag, text)] | | eChildren :: [Node tag text] | |
| Text !text | |
| Instances | |
|
|
|
Type shortcut for nodes
|
|
type Attributes tag text = [(tag, text)] | Source |
|
Type shortcut for attributes
|
|
|
Type shortcut for a single node with unqualified tag names where tag and
text are the same string type.
|
|
|
Type shortcut for nodes with unqualified tag names where tag and
text are the same string type.
|
|
|
Type shortcut for attributes with unqualified tag names where tag and
text are the same string type.
|
|
Parse to tree
|
|
|
|
|
|
|
|
|
Encoding types available for the document encoding.
| Constructors | |
|
|
|
Parse error, consisting of message text, line number, and column number
| Constructors | | Instances | |
|
|
SAX-style parse
|
|
|
|
|
|
Constructors | StartElement tag [(tag, text)] | | EndElement tag | | CharacterData text | | FailDocument XMLParseError | |
| Instances | |
|
|
|
A lower level function that lazily converts a SAX stream into a tree structure.
|
|
Variants that throw exceptions
|
|
|
An exception indicating an XML parse error, used by the ..Throwing variants.
| Constructors | | Instances | |
|
|
|
|
|
|
|
|
Abstraction of string types
|
|
|
An abstraction for any string type you want to use as xml text (that is,
attribute values or element text content). If you want to use a
new string type with hexpat, you must make it an instance of
GenericXMLString.
| | Methods | | | Instances | |
|
|
Produced by Haddock version 2.4.2 |