Skip to content

Instantly share code, notes, and snippets.

@LeifW
Created September 6, 2018 01:41
Show Gist options
  • Save LeifW/158ffe6dc1a55998cd83ef50f751aae5 to your computer and use it in GitHub Desktop.
Save LeifW/158ffe6dc1a55998cd83ef50f751aae5 to your computer and use it in GitHub Desktop.
xmlbf example of rendering an instance as XML
{-# LANGUAGE OverloadedStrings #-}
import Xmlbf
import Data.HashMap.Strict (empty)
import Data.ByteString.Builder (toLazyByteString)
import Data.Text (Text)
import qualified Data.ByteString.Lazy.Char8 as BSC
import Network.AWS.Data.Text (ToText(toText))
data Person = Person {name :: String, age :: Int} deriving Show
textElem :: ToText a => Text -> a -> Node
textElem name val = element' name empty [text $ toText val]
instance ToXml Person where
toXml (Person name age) = [
element' "person" empty [
textElem "name" name,
textElem "age" age
]
]
xml = toLazyByteString $ encode $ toXml $ Person "Bob" 40
main = BSC.putStrLn xml
@LeifW
Copy link
Author

LeifW commented Sep 6, 2018

Prints <person><name>Bob</name><age>40</age></person>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment