dom-parser-1.0.0: Simple monadic DOM parser

Safe HaskellNone
LanguageHaskell2010

Text.XML.DOM.Parser.Combinators

Contents

Synopsis

Generic combinators to traverse descendants

traverseElems Source #

Arguments

:: (Monad m, Foldable g, Traversable f) 
=> ([Element] -> DomParserT g m (f (DomPath, Element)))

Takes list of current elements and returns container with pairs of subpath (relatively to current element) and element to run parser in

-> DomParserT Identity m a

Parser will be runned for each element found in traversable

-> DomParserT g m (f a) 

Generic function to traverse arbitrary inner cursors.

inFilteredTrav Source #

Arguments

:: (Monad m, Foldable g, DomTraversable f) 
=> ([Element] -> (DomPath, [Element]))

Function returning some filtered elements with path suffixes which will be appended to parser's state

-> DomParserT Identity m a 
-> DomParserT g m (f a) 

Takes function filtering

Using DomTraversable

inElem :: (Monad m, Foldable g) => Text -> DomParserT Identity m a -> DomParserT g m a Source #

Runs parser inside first children element with given name

Dive combinators

divePath :: forall m g a. (Monad m, Foldable g) => [Text] -> DomParserT [] m a -> DomParserT g m a Source #

Dive given parser's current tags set into the given path. The divePath ["a", "b"] differs from inElem "a" . inElem "b". Namely the first variant will not fail if occured tag "a" which does not contains tag "b". This behaviour is desireable when you dont want to parse whole XML and just want to pull tags in some path. The other difference is in traversing inner elements. Consider this code

inElem "a" $ inElem "b" $ inElemAll "c" fromDom

which translates to pseudo-CSS query like: a:nth(1) > b:nth(1) > c > fromDom

divePath ["a", "b"] $ inElemAll "c" fromDom

which translates like: a > b > c > fromDom

As you can see, inElem always takes first element and runs inner parser in this single element, unlike divePath which runs inner parser in all descendants in given path.

diveElem :: (Monad m, Foldable g) => Text -> DomParserT [] m a -> DomParserT g m a Source #

Explicit ignoring elements

ignoreElem Source #

Arguments

:: Monad m 
=> (Element -> Bool)

Predicate checking that we must ignore some current tag. If returns True then parser will not be runned and combinator just returns Nothing.

-> DomParserT Identity m a 
-> DomParserT Identity m (Maybe a) 

Ignore arbitrary current element if it conforms to predicate.

ignoreEmpty :: Monad m => DomParserT Identity m a -> DomParserT Identity m (Maybe a) Source #

If current element has no children nodes does not run parser and returns Nothing. Otherwise runs parser inside current element. Usefull when you got XML with strange empty elements which must be just ignored, but inElem runs parser inside of this elements which causes to parser error.

ignoreBlank :: Monad m => DomParserT Identity m a -> DomParserT Identity m (Maybe a) Source #

If all current elements contains blank content, or contains nothing at all , then returns Nothing, else runs parser.

Getting current element's properties

getCurrentName :: Monad m => DomParserT Identity m Text Source #

Returns name of current element.

Since: 1.0.0

getCurrentContent :: Monad m => DomParserT Identity m (Maybe Text) Source #

Get current content. If current element contains no content or have inner elements then Nothing returned

Since: 1.0.0

getCurrentAttributes :: Monad m => DomParserT Identity m (Map Name Text) Source #

Retuns map of attributes of current element

Since: 1.0.0

getCurrentAttribute :: Monad m => Text -> DomParserT Identity m (Maybe Text) Source #

Returns element with given name or Nothing

Since: 1.0.0

Current element's checks

checkCurrentName :: Monad m => Text -> DomParserT Identity m () Source #

If name of current tag differs from first argument throws PENotFound with tag name replaced in last path's segment. Usefull for checking root document's element name.

Parsing element's content

parseContent Source #

Arguments

:: Monad m 
=> (Text -> Either Text a)

Content parser, return error msg if value is not parsed

-> DomParserT Identity m a 

Parses content inside current tag. It expects current element set consists of exactly ONE element. If current element does not contains content or have other elements as childs then throws error

readContent :: (Read a, Typeable a) => Text -> Either Text a Source #

Tries to read given text to value using Read. Usefull to use with parseContent and parseAttribute

maybeReadContent Source #

Arguments

:: Typeable a 
=> (Text -> Maybe a)

Content or attribute reader

-> Text

Content or attribute value

-> Either Text a 

If reader returns Nothing then resulting function returns 'Left "error message"'

Since: 1.0.0

Parsing attributes

parseAttribute Source #

Arguments

:: Monad m 
=> Text

Attribute name

-> (Text -> Either Text a)

Attribute content parser

-> DomParserT Identity m a 

Parses attribute with given name, throws error if attribute is not found.

Since: 1.0.0