hexpat-0.14: wrapper for expat, the fast XML parserSource codeContentsIndex
Text.XML.Expat.Annotated
Contents
Tree structure
Annotation-specific
Qualified nodes
Namespaced nodes
Parse to tree
Variant that throws exceptions
SAX-style parse
Abstraction of string types
Deprecated
Description

A variant of Node in which Element nodes have an annotation of any type, and some concrete functions that annotate with the XML parse location.

The names conflict with those in Tree so you must use qualified import if you want to use both modules.

Synopsis
type Node a = NodeG a []
data NodeG a c tag text
= Element {
eName :: !tag
eAttributes :: ![(tag, text)]
eChildren :: c (NodeG a c tag text)
eAnn :: a
}
| Text !text
type Attributes tag text = [(tag, text)]
type UNode a text = Node a text text
type UAttributes text = Attributes text text
type LNode tag text = Node XMLParseLocation tag text
type ULNode text = LNode text text
textContent :: (NodeClass n [], Monoid text) => n [] tag text -> text
isElement :: NodeClass n c => n c tag text -> Bool
isNamed :: (NodeClass n c, Eq tag) => tag -> n c tag text -> Bool
isText :: NodeClass n c => n c tag text -> Bool
getName :: (NodeClass n c, Monoid tag) => n c tag text -> tag
getAttributes :: NodeClass n c => n c tag text -> [(tag, text)]
getAttribute :: (NodeClass n c, GenericXMLString tag) => n c tag text -> tag -> Maybe text
getChildren :: NodeClass n c => n c tag text -> c (n c tag text)
modifyName :: NodeClass n c => (tag -> tag) -> n c tag text -> n c tag text
modifyAttributes :: NodeClass n c => ([(tag, text)] -> [(tag, text)]) -> n c tag text -> n c tag text
setAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> text -> n c tag text -> n c tag text
deleteAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> n c tag text -> n c tag text
alterAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> Maybe text -> n c tag text -> n c tag text
modifyChildren :: NodeClass n c => (c (n c tag text) -> c (n c tag text)) -> n c tag text -> n c tag text
mapAllTags :: NodeClass n c => (tag -> tag') -> n c tag text -> n c tag' text
unannotate :: Functor c => NodeG a c tag text -> NodeG c tag text
modifyAnnotation :: (a -> a) -> Node a tag text -> Node a tag text
mapAnnotation :: (a -> b) -> Node a tag text -> Node b tag text
data QName text = QName {
qnPrefix :: Maybe text
qnLocalPart :: !text
}
type QNode a text = Node a (QName text) text
type QAttributes text = Attributes (QName text) text
type QLNode text = LNode (QName text) text
toQualified :: (NodeClass n c, GenericXMLString text) => n c text text -> n c (QName text) text
fromQualified :: (NodeClass n c, GenericXMLString text) => n c (QName text) text -> n c text text
data NName text = NName {
nnNamespace :: Maybe text
nnLocalPart :: !text
}
type NNode text a = Node a (NName text) text
type NAttributes text = Attributes (NName text) text
type NLNode text = LNode (NName text) text
mkNName :: text -> text -> NName text
mkAnNName :: text -> NName text
toNamespaced :: (NodeClass n c, GenericXMLString text, Ord text, Show text) => n c (QName text) text -> n c (NName text) text
fromNamespaced :: (NodeClass n c, GenericXMLString text, Ord text, Functor c) => n c (NName text) text -> n c (QName text) text
xmlnsUri :: GenericXMLString text => text
xmlns :: GenericXMLString text => text
data ParserOptions tag text = ParserOptions {
parserEncoding :: Maybe Encoding
entityDecoder :: Maybe (tag -> Maybe text)
}
defaultParserOptions :: ParserOptions tag text
data Encoding
= ASCII
| UTF8
| UTF16
| ISO88591
parse :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> (LNode tag text, Maybe XMLParseError)
parse' :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Either XMLParseError (LNode tag text)
data XMLParseError = XMLParseError String XMLParseLocation
data XMLParseLocation = XMLParseLocation {
xmlLineNumber :: Int64
xmlColumnNumber :: Int64
xmlByteIndex :: Int64
xmlByteCount :: Int64
}
parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> LNode tag text
data XMLParseException = XMLParseException XMLParseError
data SAXEvent tag text
= StartElement tag [(tag, text)]
| EndElement tag
| CharacterData text
| FailDocument XMLParseError
saxToTree :: GenericXMLString tag => [(SAXEvent tag text, a)] -> (Node a tag text, Maybe XMLParseError)
class (Monoid s, Eq s) => GenericXMLString s where
gxNullString :: s -> Bool
gxToString :: s -> String
gxFromString :: String -> s
gxFromChar :: Char -> s
gxHead :: s -> Char
gxTail :: s -> s
gxBreakOn :: Char -> s -> (s, s)
gxFromCStringLen :: CStringLen -> IO s
gxToByteString :: s -> ByteString
eAttrs :: Node a tag text -> [(tag, text)]
parseSAX :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text]
parseSAXThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text]
parseSAXLocations :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
parseSAXLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
parseTree :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> (LNode tag text, Maybe XMLParseError)
parseTree' :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> Either XMLParseError (LNode tag text)
parseTreeThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> LNode tag text
Tree structure
type Node a = NodeG a []Source
A pure tree representation that uses a list as its container type, annotated variant.
data NodeG a c tag text Source

Annotated variant of the tree representation of the XML document, meaning that it has an extra piece of information of your choice attached to each Element.

c is the container type for the element's children, which is usually [], except when you are using chunked I/O with the hexpat-iteratee package.

tag is the tag type, which can either be one of several string types, or a special type from the Text.XML.Expat.Namespaced or Text.XML.Expat.Qualified modules.

text is the string type for text content.

a is the type of the annotation. One of the things this can be used for is to store the XML parse location, which is useful for error handling.

Note that some functions in the Text.XML.Expat.Cursor module need to create new nodes through the MkElementClass type class. Normally this can only be done if a is a Maybe type (so it can provide the Nothing value for the annotation on newly created nodes). Or, you can write your own MkElementClass instance. Apart from that, there is no requirement for a to be a Maybe type.

Constructors
Element
eName :: !tag
eAttributes :: ![(tag, text)]
eChildren :: c (NodeG a c tag text)
eAnn :: a
Text !text
show/hide Instances
(Functor c, List c) => MkElementClass (NodeG (Maybe a)) c
(Functor c, List c) => NodeClass (NodeG a) c
(Eq tag, Eq text, Eq a) => Eq (NodeG a [] tag text)
(Show tag, Show text, Show a) => Show (NodeG a [] tag text)
(NFData tag, NFData text, NFData a) => NFData (NodeG a [] tag text)
type Attributes tag text = [(tag, text)]Source
Type shortcut for attributes
type UNode a text = Node a text textSource
Type shortcut for a single annotated node with unqualified tag names where tag and text are the same string type
type UAttributes text = Attributes text textSource
Type shortcut for attributes with unqualified names where tag and text are the same string type.
type LNode tag text = Node XMLParseLocation tag textSource
Type shortcut for a single annotated node, annotated with parse location
type ULNode text = LNode text textSource
Type shortcut for a single node with unqualified tag names where tag and text are the same string type, annotated with parse location
textContent :: (NodeClass n [], Monoid text) => n [] tag text -> textSource
Extract all text content from inside a tag into a single string, including any text contained in children.
isElement :: NodeClass n c => n c tag text -> BoolSource
Is the given node an element?
isNamed :: (NodeClass n c, Eq tag) => tag -> n c tag text -> BoolSource
Is the given node a tag with the given name?
isText :: NodeClass n c => n c tag text -> BoolSource
Is the given node text?
getName :: (NodeClass n c, Monoid tag) => n c tag text -> tagSource
Get the name of this node if it's an element, return empty string otherwise.
getAttributes :: NodeClass n c => n c tag text -> [(tag, text)]Source
Get the attributes of a node if it's an element, return empty list otherwise.
getAttribute :: (NodeClass n c, GenericXMLString tag) => n c tag text -> tag -> Maybe textSource
Get the value of the attribute having the specified name.
getChildren :: NodeClass n c => n c tag text -> c (n c tag text)Source
Get children of a node if it's an element, return empty list otherwise.
modifyName :: NodeClass n c => (tag -> tag) -> n c tag text -> n c tag textSource
Modify name if it's an element, no-op otherwise.
modifyAttributes :: NodeClass n c => ([(tag, text)] -> [(tag, text)]) -> n c tag text -> n c tag textSource
Modify attributes if it's an element, no-op otherwise.
setAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> text -> n c tag text -> n c tag textSource
Set the value of the attribute with the specified name to the value, overwriting the first existing attribute with that name if present.
deleteAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> n c tag text -> n c tag textSource
Delete the first attribute matching the specified name.
alterAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> Maybe text -> n c tag text -> n c tag textSource
setAttribute if Just, deleteAttribute if Nothing.
modifyChildren :: NodeClass n c => (c (n c tag text) -> c (n c tag text)) -> n c tag text -> n c tag textSource
Modify children (non-recursively) if it's an element, no-op otherwise.
mapAllTags :: NodeClass n c => (tag -> tag') -> n c tag text -> n c tag' textSource
Map all tags (both tag names and attribute names) recursively.
Annotation-specific
unannotate :: Functor c => NodeG a c tag text -> NodeG c tag textSource
Convert an annotated tree (Annotated module) into a non-annotated tree (Tree module). Needed, for example, when you format your tree to XML, since format takes a non-annotated tree.
modifyAnnotation :: (a -> a) -> Node a tag text -> Node a tag textSource
Modify this node's annotation (non-recursively) if it's an element, otherwise no-op.
mapAnnotation :: (a -> b) -> Node a tag text -> Node b tag textSource
Modify this node's annotation and all its children recursively if it's an element, otherwise no-op.
Qualified nodes
data QName text Source

A qualified name.

Qualified names have two parts, a prefix and a local part. The local part is the name of the tag. The prefix scopes that name to a particular group of legal tags.

The prefix will usually be associated with a namespace URI. This is usually achieved by using xmlns attributes to bind prefixes to URIs.

Constructors
QName
qnPrefix :: Maybe text
qnLocalPart :: !text
show/hide Instances
Eq text => Eq (QName text)
Show text => Show (QName text)
NFData text => NFData (QName text)
type QNode a text = Node a (QName text) textSource
Type shortcut for a single annotated node where qualified names are used for tags
type QAttributes text = Attributes (QName text) textSource
Type shortcut for attributes with qualified names
type QLNode text = LNode (QName text) textSource
Type shortcut for a single node where qualified names are used for tags, annotated with parse location
toQualified :: (NodeClass n c, GenericXMLString text) => n c text text -> n c (QName text) textSource
fromQualified :: (NodeClass n c, GenericXMLString text) => n c (QName text) text -> n c text textSource
Namespaced nodes
data NName text Source

A namespace-qualified tag.

NName has two components, a local part and an optional namespace. The local part is the name of the tag. The namespace is the URI identifying collections of declared tags. Tags with the same local part but from different namespaces are distinct. Unqualified tags are those with no namespace. They are in the default namespace, and all uses of an unqualified tag are equivalent.

Constructors
NName
nnNamespace :: Maybe text
nnLocalPart :: !text
show/hide Instances
Eq text => Eq (NName text)
Show text => Show (NName text)
NFData text => NFData (NName text)
type NNode text a = Node a (NName text) textSource
Type shortcut for a single annotated node where namespaced names are used for tags
type NAttributes text = Attributes (NName text) textSource
Type shortcut for attributes with namespaced names
type NLNode text = LNode (NName text) textSource
Type shortcut for a single node where namespaced names are used for tags, annotated with parse location
mkNName :: text -> text -> NName textSource
Make a new NName from a prefix and localPart.
mkAnNName :: text -> NName textSource
Make a new NName with no prefix.
toNamespaced :: (NodeClass n c, GenericXMLString text, Ord text, Show text) => n c (QName text) text -> n c (NName text) textSource
fromNamespaced :: (NodeClass n c, GenericXMLString text, Ord text, Functor c) => n c (NName text) text -> n c (QName text) textSource
xmlnsUri :: GenericXMLString text => textSource
xmlns :: GenericXMLString text => textSource
Parse to tree
data ParserOptions tag text Source
Constructors
ParserOptions
parserEncoding :: Maybe EncodingThe encoding parameter, if provided, overrides the document's encoding declaration.
entityDecoder :: Maybe (tag -> Maybe text)If provided, entity references (i.e.   and friends) will be decoded into text using the supplied lookup function
defaultParserOptions :: ParserOptions tag textSource
data Encoding Source
Encoding types available for the document encoding.
Constructors
ASCII
UTF8
UTF16
ISO88591
parseSource
:: (GenericXMLString tag, GenericXMLString text)
=> ParserOptions tag textOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> (LNode tag text, Maybe XMLParseError)
Lazily parse XML to tree. Note that forcing the XMLParseError return value will force the entire parse. Therefore, to ensure lazy operation, don't check the error status until you have processed the tree.
parse'Source
:: (GenericXMLString tag, GenericXMLString text)
=> ParserOptions tag textOptional encoding override
-> ByteStringInput text (a strict ByteString)
-> Either XMLParseError (LNode tag text)
Strictly parse XML to tree. Returns error message or valid parsed tree.
data XMLParseError Source
Parse error, consisting of message text and error location
Constructors
XMLParseError String XMLParseLocation
show/hide Instances
data XMLParseLocation Source
Specifies a location of an event within the input text
Constructors
XMLParseLocation
xmlLineNumber :: Int64Line number of the event
xmlColumnNumber :: Int64Column number of the event
xmlByteIndex :: Int64Byte index of event from start of document
xmlByteCount :: Int64The number of bytes in the event
show/hide Instances
Variant that throws exceptions
parseThrowingSource
:: (GenericXMLString tag, GenericXMLString text)
=> ParserOptions tag textOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> LNode tag text

Lazily parse XML to tree. In the event of an error, throw XMLParseException.

parseThrowing can throw an exception from pure code, which is generally a bad way to handle errors, because Haskell's lazy evaluation means it's hard to predict where it will be thrown from. However, it may be acceptable in situations where it's not expected during normal operation, depending on the design of your program.

data XMLParseException Source
An exception indicating an XML parse error, used by the ..Throwing variants.
Constructors
XMLParseException XMLParseError
show/hide Instances
SAX-style parse
data SAXEvent tag text Source
Constructors
StartElement tag [(tag, text)]
EndElement tag
CharacterData text
FailDocument XMLParseError
show/hide Instances
(Eq tag, Eq text) => Eq (SAXEvent tag text)
(Show tag, Show text) => Show (SAXEvent tag text)
(NFData tag, NFData text) => NFData (SAXEvent tag text)
saxToTree :: GenericXMLString tag => [(SAXEvent tag text, a)] -> (Node a tag text, Maybe XMLParseError)Source
A lower level function that lazily converts a SAX stream into a tree structure. Variant that takes annotations for start tags.
Abstraction of string types
class (Monoid s, Eq s) => GenericXMLString s whereSource
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
gxNullString :: s -> BoolSource
gxToString :: s -> StringSource
gxFromString :: String -> sSource
gxFromChar :: Char -> sSource
gxHead :: s -> CharSource
gxTail :: s -> sSource
gxBreakOn :: Char -> s -> (s, s)Source
gxFromCStringLen :: CStringLen -> IO sSource
gxToByteString :: s -> ByteStringSource
show/hide Instances
Deprecated
eAttrs :: Node a tag text -> [(tag, text)]Source
parseSAXSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> [SAXEvent tag text]

DEPRECATED: Use parse instead.

Lazily parse XML to SAX events. In the event of an error, FailDocument is the last element of the output list. Deprecated in favour of new parse

parseSAXThrowingSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> [SAXEvent tag text]

DEPRECATED: Use parseThrowing instead.

Lazily parse XML to SAX events. In the event of an error, throw XMLParseException.

parseSAXLocationsSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> [(SAXEvent tag text, XMLParseLocation)]

DEPRECATED: Use parseLocations instead.

A variant of parseSAX that gives a document location with each SAX event.

parseSAXLocationsThrowingSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> [(SAXEvent tag text, XMLParseLocation)]

DEPRECATED: Used parseLocationsThrowing instead.

A variant of parseSAX that gives a document location with each SAX event. In the event of an error, throw XMLParseException.

parseTreeSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> (LNode tag text, Maybe XMLParseError)

DEPRECATED: Use parse instead.

Lazily parse XML to tree. Note that forcing the XMLParseError return value will force the entire parse. Therefore, to ensure lazy operation, don't check the error status until you have processed the tree.

parseTree'Source
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a strict ByteString)
-> Either XMLParseError (LNode tag text)

DEPRECATED: use parse instead.

Strictly parse XML to tree. Returns error message or valid parsed tree.

parseTreeThrowingSource
:: (GenericXMLString tag, GenericXMLString text)
=> Maybe EncodingOptional encoding override
-> ByteStringInput text (a lazy ByteString)
-> LNode tag text

DEPRECATED: use parseThrowing instead

Lazily parse XML to tree. In the event of an error, throw XMLParseException.

Produced by Haddock version 2.6.1