| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Text.XML.Hexml
Description
A module for fast first-approximation parsing of XML.
Note that entities, e.g. &, are not expanded.
- data Node
- data Attribute = Attribute {}
- parse :: ByteString -> Either ByteString Node
- render :: Node -> ByteString
- location :: Node -> (Int, Int)
- name :: Node -> ByteString
- inner :: Node -> ByteString
- outer :: Node -> ByteString
- attributes :: Node -> [Attribute]
- children :: Node -> [Node]
- contents :: Node -> [Either ByteString Node]
- attributeBy :: Node -> ByteString -> Maybe Attribute
- childrenBy :: Node -> ByteString -> [Node]
Documentation
An XML attribute, comprising of a name and a value. As an example,
hello="world" would produce Attribute "hello" "world".
Constructors
| Attribute | |
Fields | |
parse :: ByteString -> Either ByteString Node Source
Parse a ByteString as an XML document, returning a Left error message, or a Right document.
Note that the returned node will have a name of "", no attributes, and contents as per the document.
Often the first child will be the <?xml ... ?> element. For documents which comprise an XML node and a single
root element, use .children n !! 1
render :: Node -> ByteString Source
Given a node, rerender it to something with an equivalent parse tree.
Mostly useful for debugging - if you want the real source document use outer instead.
location :: Node -> (Int, Int) Source
Find the starting location of a node, the < character.
The first character will be reported as (line 1,column 1), because thats
how error messages typically do it.
name :: Node -> ByteString Source
Get the name of a node, e.g. <test /> produces "test".
inner :: Node -> ByteString Source
Get the inner text, from inside the tag, e.g. <test /> produces ""
and <test>hello</test> produces "hello".
The result will have identical layout/spacing to the source document.
outer :: Node -> ByteString Source
Get the outer text, including the tag itself, e.g. <test /> produces "<test />"
and <test>hello</test> produces "<test>hello</test>".
The result will have identical layout/spacing to the source document.
attributes :: Node -> [Attribute] Source
Get the attributes of this node.
attributeBy :: Node -> ByteString -> Maybe Attribute Source
Get the first attribute of this node which has a specific name, if there is one. A more efficient version of:
attributeBy n s = listToMaybe $ filter (\(Attribute a _) -> a == s $ attributes n
childrenBy :: Node -> ByteString -> [Node] Source
Get the direct children of this node which have a specific name. A more efficient version of:
childrenBy p s = filter (\n -> name n == s) $ children p