Advertisement
Guest User

Untitled

a guest
Feb 6th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. (defn render
  2. "Do the hard work for core/xml"
  3. [document xmlns nss nodes]
  4. (letfn [(make-node [root [tag & nodes :as total]]
  5. (let [prefix (namespace tag)
  6. tag (name tag)]
  7. (let [ele (if-let [uri (and prefix (get nss prefix))]
  8. (.createElementNS document uri (str prefix \: tag))
  9. (.createElement document tag))]
  10. (doseq [[k v] (meta total)]
  11. (.setAttribute ele (name k) (str v)))
  12. (reduce
  13. (fn [f child]
  14. (fn []
  15. (.appendChild ele (inner-render ele child))
  16. f))
  17. #(do (.appendChild root ele) ele)
  18. nodes))))
  19.  
  20. (inner-render [root node]
  21. (cond
  22. (coll? node) (make-node root node)
  23. (string? node) (.appendChild root (.createTextNode document node))
  24. (instance? org.w3c.dom.Element node) (.appendChild root node)
  25. (fn? node) node
  26. :else (recur root (str node))))]
  27.  
  28. (let [root (.getDocumentElement document)]
  29. (doseq [node nodes]
  30. (.appendChild root (trampoline inner-render root node))))
  31. document))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement