开发者

How do reimplement this Python XML-parsing function in Haskell?

I recently wrote the following Python function which will take a Google Picasa contacts.xml f开发者_如何学Pythonile and output a dictionary with ID and Name.

def read_contacts_file(fn):
    import xml.etree.ElementTree
    x = xml.etree.ElementTree.ElementTree(file=fn)
    q = [(u.attrib["id"], u.attrib["name"]) for u in x.iter("contact")]
    return dict(q)

What this function does is return a dictionary (hashtable, map) with the ID being the key and the Name being the value.

The file itself has the form:

<contacts>
 <contact id="f5fdaaee2e80fa01" name="Person A" display="A"/>
 <contact id="8d5256298fd43877" name="Person B" display="B"/>
</contacts>

What's the simplest way I can implement this in Haskell?


Just wanted to let you know that with everyone's help, I've managed to come up with the following which makes sense to me. Thanks.

parseContactsData = M.fromList . runLA (xread >>> f) 
    where f = 
        getChildren 
        >>> hasName "contact" 
        >>> getAttrValue "id" &&& getAttrValue "name"


Here's a minimal example doing the same thing with tagsoup:

import Text.HTML.TagSoup

assocLookup k dict = [v | (k', v) <- dict, k == k']
readContactsFile fn = fmap parse (readFile fn)

parse contents = do
    TagOpen "contact" attrs <- parseTags contents
    id <- assocLookup "id" attrs
    name <- assocLookup "name" attrs
    return (id, name)


Here's an example doing it with HXT

import Text.XML.HXT.Core
import Data.Map

idAssocs = hasName "contact" >>> getAttrValue "id" &&& getAttrValue "name"
dict = fromList `fmap` runX (readDocument [] "contacts.xml" >>> deep idAssocs)


One alternative would be to use the HXT library. Here are some useful information which will get you started: http://www.haskell.org/haskellwiki/HXT/Conversion_of_Haskell_data_from/to_XML

Particularly useful for you would probably be looking at 3.5 A simple application

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜