Idiomatic way to build a custom structure from XML zipper in Clojure
Posted
by Checkers
on Stack Overflow
See other posts from Stack Overflow
or by Checkers
Published on 2010-05-07T11:20:52Z
Indexed on
2010/05/07
11:48 UTC
Read the original article
Hit count: 314
Say, I'm parsing an RSS feed and want to extract a subset of information from it.
(def feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse))
I can get links and titles separately:
(xml-> feed :channel :item :link text)
(xml-> feed :channel :item :title text)
However I can't figure out the way to extract them at the same time without traversing the zipper more than once, e.g.
(let [feed (-> "http://..." clojure.zip/xml-zip clojure.xml/parse)]
(zipmap
(xml-> feed :channel :item :link text)
(xml-> feed :channel :item :title text)))
...or a variation of thereof, involving mapping multiple sequences to a function that incrementally builds a map with, say, assoc
.
Not only I have to traverse the sequence multiple times, the sequences also have separate states, so elements must be "aligned", so to speak. That is, in a more complex case than RSS, a sub-element may be missing in particular element, making one of sequences shorter by one (there are no gaps). So the result may actually be incorrect.
Is there a better way or is it, in fact, the way you do it in Clojure?
© Stack Overflow or respective owner