Skip to content

Instantly share code, notes, and snippets.

@dsedivec
Created October 1, 2009 23:04
Show Gist options
  • Save dsedivec/199309 to your computer and use it in GitHub Desktop.
Save dsedivec/199309 to your computer and use it in GitHub Desktop.
Example of using c.c.zip-filter.xml
(require '[clojure.xml :as xml]
'[clojure.zip :as zip]
'[clojure.contrib.zip-filter.xml :as zfx])
(def reddit-rss-xml (xml/parse "http://www.reddit.com/.rss"))
(def zipped-xml (zip/xml-zip reddit-rss-xml))
;; xml1-> treats keywords as "search for this tag." xml/node at the
;; end says "return me that one part of the XML map" (format specified
;; by xml/parse).
;;
;; Psuedo-code for the below statement might be something like:
;; (xml/node (find-child-with-tag "title" (find-child-with-tag "channel" zipped-xml)))
(zfx/xml1-> zipped-xml :channel :title xml/node)
;; => {:tag :title, :attrs nil, :content ["reddit.com: what's new online!"]}
;;
;; This is the map format returned by xml/parse, with keys :tag,
;; :attrs, :content. You could call (:content) on this result, for
;; example, to get the vector containing all the children of this
;; node.
;; Just want the text? zip-filter.xml has (text) for that:
(zfx/xml1-> zipped-xml :channel :title zfx/text)
;; => "reddit.com: what's new online!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment