Idiomatic way to build a custom structure from XML zipper in Clojure
- by Checkers
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?