'XML declaration with xmlbf

It is necessary to develop an api that would use the xml format for responses.

<?xml version="1.0" encoding="UTF-8" ?>
<response>
  <result>0</result>  
</response>

I am using the servant and servant-xml libraries.

newtype Response = Response
  { result :: Int }

instance ToXml Response where
  toXml r = element "response" HM.empty 
    $ element "result" HM.empty (text $ T.pack . show $ result r)

type API = Get '[XML] Response

proxy :: Proxy API
proxy = Proxy

server :: Server API
server = pure $ Response 0

main :: IO ()
main = run 8080 $ serve proxy server

and get

<response>
  <result>0</result>
</response>

xmlbf library (which servant-xml uses) has the ability to work with nodes and elements (element and text functions from the example above). Is it possible to add xml declaration using xmlbf?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source