Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 file 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"
like image 624
Snoqual Avatar asked Dec 09 '22 07:12

Snoqual


1 Answers

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)
like image 76
Daniel Wagner Avatar answered Mar 01 '23 23:03

Daniel Wagner