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"
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With