Last active
May 6, 2016 16:19
-
-
Save kul/60c64e9180b4ebcd38c565861cc37dd3 to your computer and use it in GitHub Desktop.
XML Maniplulation in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; [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