Skip to content

Instantly share code, notes, and snippets.

@kul
Last active May 6, 2016 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kul/60c64e9180b4ebcd38c565861cc37dd3 to your computer and use it in GitHub Desktop.
Save kul/60c64e9180b4ebcd38c565861cc37dd3 to your computer and use it in GitHub Desktop.
XML Maniplulation in Clojure
; [clojure.zip :as zip]
; [clojure.data.xml :refer [parse-str] :as x]
; [clojure.data.zip.xml :refer [xml-> text] :as zx]
(defn parse-xml
[s]
(-> s x/parse-str zip/xml-zip))
(defn update-in-xml
[xml ks f & args]
(let [loc (apply zx/xml1-> xml ks)]
(-> (apply zip/edit loc f args)
zip/root zip/xml-zip)))
(defn xml-to-str
[xml]
(x/emit-str (zip/root xml)))
(let [xml (parse-xml "<a><b>1</b></a>")
xml (update-in-xml xml [:a :b] assoc :content 2 :tag :c)]
(xml-to-str xml))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment