xml-conduit-writer- Warm and fuzzy creation of XML documents.

Safe HaskellNone




Overcome XML insanity, node by node.

 {-# LANGUAGE OverloadedStrings #-}

 let doc = document "root" $ do
     element "hello" $ content "world"
     element "hierarchy" $ do
         element "simple" True
         element "as" ("it should be" :: Text)
         toXML $ Just . T.pack $ "like this"
     comment "that's it!"





:: Name

Root node name

-> XML


-> Document 

Create a simple Document starting with a root element.

soap :: (ToXML h, ToXML b) => h -> b -> DocumentSource

Generate a SOAPv1.1 document.

Empty header will be ignored. Envelope uses a soapenv prefix. Works great with ToXML class.

 data BigData = BigData { webScale :: Bool }
 instance ToXML BigData where
     toXML (BigData ws) = element ("v" !: "{vendor:uri}bigData") $ toXML ws
 let doc = soap () (BigData True)

pprint :: Document -> IO ()Source

Render document using xml-conduit's pretty-printer.


type XML = Writer (DList Node) ()Source

Node container to be rendered as children nodes.

Node creation

node :: Node -> XMLSource

Insert one node.

instruction :: Text -> Text -> XMLSource

Insert an Instruction node.

comment :: Text -> XMLSource

Insert a text comment node.

element :: ToXML a => Name -> a -> XMLSource

Insert an Element node constructed with name and children.

elementMaybe :: ToXML a => Name -> Maybe a -> XMLSource

Insert an Element node converted from Maybe value or do nothing.

elementA :: ToXML a => Name -> [(Name, Text)] -> a -> XMLSource

Insert an Element node constructed with name, attributes and children.

content :: Text -> XMLSource

Insert text content node.

empty :: XMLSource

Do nothing.



:: ToXML a 
=> Name

Container element name.

-> [a]

Items to convert.

-> XML 

Mass-convert to nodes.

 let array = element "container" $ many "wrapper" [1..3]

Which gives:


Use `mapM_ toXML xs` to convert a list without wrapping each item in separate element.

 let mess = element "container" $ mapM_ toXML ["chunky", "chunk"]

Content nodes tend to glue together:


Element helpers

render :: XML -> [Node]Source

Convert collected nodes to a list of child nodes.

(!:) :: Text -> Name -> NameSource

Attach a prefix to a Name.

Because simply placing a colon in an element name yields Nothing as a prefix and children will revert to en empty namespace.

Converting data

class ToXML a whereSource

Provide instances for this class to use your data as XML nodes.


toXML :: a -> XMLSource


ToXML Bool

XML schema uses lower case.

ToXML Char 
ToXML Double 
ToXML Float 
ToXML Int 
ToXML Integer 
ToXML ()

Do nothing.

ToXML Text

Don't use [Char] please, it will scare OverloadedStrings.

ToXML Text 

Insert already prepared nodes.

ToXML a => ToXML (Maybe a)

Insert node if available. Otherwise do nothing.