How can I generate XML using Haskell?

Jonathan

I'm looking for a way to generate XML like in the example below, but the existing XML libraries don't provide adequate documentation for me to understand how to use them to generate XML. I can find plenty of XML-generating libraries, but none with a simple example which says: here's how to generate this XML.

The Blaze and Lucid libraries are great for generating HTML, for example. Let's say you want to make this HTML:

<emph class="foo">bar</emph>

Using Lucid, you would write:

emph_ [class_ "foo"] "bar" 

So what's a good way to do this with XML? I've been looking through the API documentation for, for instance, HaXml. But I can't find many tutorials about using those packages.

I did see that Yesod's Hamlet quasi-quoter is a very succinct way of generating XML. But I don't like the idea of quasi-quoting up a new schema, since it doesn't seem as maintable, and seems like learning a new language. So I'm hoping to find something more modular, and composable, like Blaze and Lucid.

Edit: To explain further, the problem is not a lack of Haskell XML libraries, or knowing which one to use. It's knowing how to use one (any of them) to generate XML. For instance, I know I can generate the HTML code <html>foo</html> using Lucid's html_ "foo". But how can I do that for XML?

Here's a pseudo-Haskell example of what I'm trying to do:

Objective: generate the following XML:

<foo attribute="something">
  <bar>
     <foobar>
        <barfoo>
           something here
        </barfoo>
     </foobar>
  </bar>
</foo>

Pseudo-Haskell:

foo_ [attribute_ "bar"] $ bar_ $ foobar_ $ barfoo_ "something here" 
K. A. Buhr

At the start of the Yesod Book documentation on xml-hamlet, they show how to generate XML using the Text.XML API from xml-conduit. So, literally, the "way you use (any of them) to generate XML" is:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}

import qualified Data.Text.Lazy.IO as Text
import qualified Data.Map as Map
import Text.XML

myFoo = Element "foo" [("attribute", "something")]
  [ NodeElement $ Element "bar" []
    [ NodeElement $ Element "foobar" []
      [ NodeElement $ Element "barfoo" []
        [ NodeContent "something here" ]]]]

main = Text.putStrLn $ renderText def $ Document (Prologue [] Nothing []) myFoo []

giving:

> main
<?xml version="1.0" encoding="UTF-8"?><foo attribute="something">
<bar><foobar><barfoo>something here</barfoo></foobar></bar></foo>

The main issue is syntax, but it's really not that difficult to write your own lucid-like DSL using plain Haskell functions. If you want a completely flexible syntax that's schema agnostic, you could write:

-- helpers
attr_ = (,)
element_ nam attrs bdy = NodeElement $ Element nam (Map.fromList attrs) bdy
text_ = NodeContent

-- elements
foo_ = element_ "foo"
bar_ = element_ "bar"
foobar_ = element_ "foobar"
barfoo_ = element_ "barfoo_"

-- attributes
attribute_ = attr_ "attribute"

-- used to unwrap a top level Node to an Element
unNode (NodeElement x) = x

myFoo = unNode $
  foo_ [attribute_ "something"] $
    [ bar_ []
      [ foobar_ []
        [ barfoo_ []
          [ text_ "something here" ]]]]

main = Text.putStrLn $ renderText def $ Document (Prologue [] Nothing []) myFoo []

Usually the syntax can be simplified by considering your (informal) schema. If barfoos have no attributes and can only contain a block of text, then you write:

barfoo_ txt = element_ "barfoo" [] [text_ txt]

and use it as barfoo_ "something here" instead.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can i generate not specified data from xml while deserialize it?

分類Dev

How Can i Parse XML using Python

分類Dev

How can I elegantly do not . any in Haskell?

分類Dev

How can I generate UUID in C#

分類Dev

How can I generate hash codes with Scala

分類Dev

How can I generate marginal effects for a logit model when using survey weights?

分類Dev

How can I generate classes from outside XSD file using JAXB XJC

分類Dev

How can I generate a description list (dl) in Vue.js using v-for?

分類Dev

How can I generate all the possible binary lists with 4 elements? (Using Python)

分類Dev

How can i generate pdf using xsl-fo footer and header?

分類Dev

Generate xml using LINQ to XML

分類Dev

how i can execute scripts of multiple classes in xml file using TestNG frame work

分類Dev

How can I group and sum XML data with the following structure using LINQ?

分類Dev

How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

分類Dev

How can I get the values of XML nodes depending on the content of another node by using XSLT?

分類Dev

How can I make a Pipe concurrent with Haskell's Pipe library?

分類Dev

How can I generate input[type=date] with simple_form?

分類Dev

In Flask, how can I generate a dynamic URL upon button click?

分類Dev

How can I use a variable to generate barcode with python-barcode

分類Dev

how can I generate a list of days by known months

分類Dev

How can I generate an unique id in Symfony 4?

分類Dev

How can I get Powershell to generate logs after moving files?

分類Dev

How can I generate and knit personalized versions of an .Rmd report?

分類Dev

How can I generate a "Social Golfer" matrix for worker seating arrangement?

分類Dev

How can I generate email statistics from mutt header cache?

分類Dev

How can one generate and save a file client side using Blazor?

分類Dev

How do I generate PuLP variables and constrains without using exec?

分類Dev

How can I load many (100K+) XML documents using mlcp without encountering "argument list too long" error?

分類Dev

How can I parse values from a dynamic webpage using Excel VBA when XML/XPath doesn't seem to work?

Related 関連記事

  1. 1

    How can i generate not specified data from xml while deserialize it?

  2. 2

    How Can i Parse XML using Python

  3. 3

    How can I elegantly do not . any in Haskell?

  4. 4

    How can I generate UUID in C#

  5. 5

    How can I generate hash codes with Scala

  6. 6

    How can I generate marginal effects for a logit model when using survey weights?

  7. 7

    How can I generate classes from outside XSD file using JAXB XJC

  8. 8

    How can I generate a description list (dl) in Vue.js using v-for?

  9. 9

    How can I generate all the possible binary lists with 4 elements? (Using Python)

  10. 10

    How can i generate pdf using xsl-fo footer and header?

  11. 11

    Generate xml using LINQ to XML

  12. 12

    how i can execute scripts of multiple classes in xml file using TestNG frame work

  13. 13

    How can I group and sum XML data with the following structure using LINQ?

  14. 14

    How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

  15. 15

    How can I get the values of XML nodes depending on the content of another node by using XSLT?

  16. 16

    How can I make a Pipe concurrent with Haskell's Pipe library?

  17. 17

    How can I generate input[type=date] with simple_form?

  18. 18

    In Flask, how can I generate a dynamic URL upon button click?

  19. 19

    How can I use a variable to generate barcode with python-barcode

  20. 20

    how can I generate a list of days by known months

  21. 21

    How can I generate an unique id in Symfony 4?

  22. 22

    How can I get Powershell to generate logs after moving files?

  23. 23

    How can I generate and knit personalized versions of an .Rmd report?

  24. 24

    How can I generate a "Social Golfer" matrix for worker seating arrangement?

  25. 25

    How can I generate email statistics from mutt header cache?

  26. 26

    How can one generate and save a file client side using Blazor?

  27. 27

    How do I generate PuLP variables and constrains without using exec?

  28. 28

    How can I load many (100K+) XML documents using mlcp without encountering "argument list too long" error?

  29. 29

    How can I parse values from a dynamic webpage using Excel VBA when XML/XPath doesn't seem to work?

ホットタグ

アーカイブ