[Haskell-cafe] XmlSerializer.deserialize?

Hugh Perkins hughperkins at gmail.com
Sun Jun 24 08:14:42 EDT 2007


Hi,

Trying to write a function to deserialize a haskell type from xml.

Ideally this wont need a third "DTD" file, ie it will work something like
XmlSerializer.Deserialize from C#:

deserializeXml :: Data(a) => String -> a
serializeXml :: Data(a) => a -> String

Writing serializeXml is pretty easy:

import Data.Generics

-- helper function from http://www.defmacro.org/ramblings/haskell-web.html
introspectData :: Data a => a -> [(String
<http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String>,
String <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String>)]
introspectData a = zip
<http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip>
fields (gmapQ gshow a)
    where fields = constrFields $
<http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
toConstr a

-- function to create xml string from single-layer Haskell data type
serializeXml object = "<" ++
<http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
show <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show>(toConstr
object) ++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
">" ++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
   foldr <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldr>
(\(a,b) x  -> x ++
<http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
"<" ++ a ++ ">" ++ b ++ "</" ++ a ++ ">") "" ( introspectData object )
   ++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
"</" ++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
show <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show>(toConstr
object) ++ <http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:.>
">"


... however cant figure out how to go the other way.

Usage of haxml or HXT to achieve this is ok, whatever it takes ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070624/3d986c54/attachment.htm


More information about the Haskell-Cafe mailing list