Created
February 17, 2021 08:13
-
-
Save malcolmsparks/0aa99abcffcadd652a517d34f0be73e0 to your computer and use it in GitHub Desktop.
XML DOM builder 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
(defmacro dom [doc parent el] | |
(let [docsym (gensym "doc") | |
parentsym (gensym "parent") | |
elsym (gensym "el")] | |
`(let [~parentsym ~parent | |
[ens# enm#] ~(first el) | |
~docsym ~doc | |
~elsym (.createElementNS ~docsym ens# enm#)] | |
~@(concat | |
(for [item (next el)] | |
(cond | |
(string? item) `(.appendChild ~elsym (.createTextNode ~docsym ~item)) | |
(list? item) `(.appendChild ~elsym (dom ~docsym ~parentsym ~item)))) | |
[`(.appendChild ~parentsym ~elsym)])))) | |
(let [doc (.newDocument dom-builder)] | |
(dom | |
doc doc | |
(["DAV:" "multistatus"] | |
(["DAV:" "response"] | |
(["DAV:" "href"] "http://www.example.com/file") | |
(["DAV:" "propstat"] | |
(["DAV:" "prop"] | |
(["DAV:" "bigbox"]))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment