开发者

Idiomatic way to build a custom structure from XML zipper in Clojure

开发者 https://www.devze.com 2022-12-29 06:04 出处:网络
Say, I\'m parsing an RSS feed and want to extract a 开发者_JAVA技巧subset of information from it.

Say, I'm parsing an RSS feed and want to extract a 开发者_JAVA技巧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?


What about this?

(reduce (fn [h item] 
          (assoc h (xml1-> item :title text) 
                   (xml1-> item :link text))) 
        {} (xml-> feed :channel :item))
0

精彩评论

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