Skip to content

Instantly share code, notes, and snippets.

@malcolmsparks
Created February 17, 2021 08:13
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 malcolmsparks/0aa99abcffcadd652a517d34f0be73e0 to your computer and use it in GitHub Desktop.
Save malcolmsparks/0aa99abcffcadd652a517d34f0be73e0 to your computer and use it in GitHub Desktop.
XML DOM builder in Clojure
(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