Created
September 6, 2018 01:41
-
-
Save LeifW/158ffe6dc1a55998cd83ef50f751aae5 to your computer and use it in GitHub Desktop.
xmlbf example of rendering an instance as XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints
<person><name>Bob</name><age>40</age></person>