Headline

XML data binding for Language:Haskell with Technology:HXT's Technology:XML pickler

Motivation

The implementation demonstrates XML data binding for Language:Haskell with Technology:HXT's Technology:XML pickler. As a result, companies can be imported from and exported to Language:XML while using algebraic data types for companies in the core functional programming. The mapping between XML and algebraic data type relies on Technology:XML pickler's type class

XMLPickler
for type-specific pickler functions. Type-class instances are defined for the types for companies, departments, and employees. These definitions leverage Technology:XML pickler's predefined arrows for picklers, predefined picklers and pickler combinators.

Illustration

A Pickler for Companies

To define a pickler for companies we declare an instance of

XMLPickler
:

instance XmlPickler Company where
    xpickle = xpCompany

The pickler function

xpCompany
is defined as follows:

xpCompany :: PU Company
xpCompany 
      = xpElem "company" $
        xpWrap ( uncurry Company
               , \c -> ( cname c
                       , depts c
                       )
                ) $
        xpPair  (xpAttr "name" xpText)
                (xpList xpickle)

For implementing

xpCompany
we use
xpElem
in line 3. By using this function we define that a company, represented in XML, should be inside a XML tag labeled "company". The pickler for the content of the tag is defined by the second argument of
xpElem
.

xpWrap
is of type
(a -> b, b -> a) -> PU a -> PU b
. It returns a Pickler (PU) for
b
and expects a pair of functions from
a
to
b
and vice versa and a Pickler for
a
(
PA a
).

In lines 4-8 the first part of the first argument of the wrapping pickler is

uncurry Company
. It defines how to construct a
Company
value from a pair of name and departments. The second part of the pair defines the opposite direction: How to disassemble a company into its components.

The second argument of

xpWrap
in lines 9-10 defines the actual pickler for the (name, departments) pair (this is
PA a
in the type signature of the wrapping pickler). We use the combinator for pairs
xpPair :: PU a -> PU b -> PU (a, b)
. The pickler for the company name is defined by using a pickler for XML attributes and a pickler for text. That is, the company name should be an attribute of the "company" tag. The pickler for the list of departments is defined by making use of a combinator for lists and
xpickle
. Because of type inference and because we also declare a
XMLPickler
instance for departments, Haskell will choose the appropriate pickler function for departments.

Pickling a Company

Pickling a company is realized by using arrows:

runX ( constA company 
       >>>
       xpickleDocument xpCompany [withIndent yes] $ 
       "sampleCompanyCut.xml"
     )

A lifted sample company is passed to the arrow for pickling a document. In this case

xpickleDocument
expects a pickler for companies, some writing options and a file name.

Unpickling a Company

To unpickle a company the arrow function

xunpickleDocument
is used:

[company1] <- runX ( xunpickleDocument xpCompany $ 
                     [withRemoveWS yes] $ 
                     "sampleCompany.xml" )

runX
returns a list of arrow results, which we except to be a singleton list. On success
company1
should hold the unpicklered company.

Architecture

this!!Pickler.hs holds the pickler definitions for companies, departments and employees. The algebraic data type for companies can be found in this!!Company.hs.this!!Total.hs and this!!Cut.hs provide totaling and cutting functionality. this!!sampleCompany.xml holds a sample company. this!!Main.hs collects test scenarios for pickling/unpickling, totaling and cutting companies.

Usage

function has to be applied. One can also use the this!!Makefile with a target test for test automation.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.