• (emit-element e)

1 Example top

  • (use 'clojure.xml)
    ;;If just a string it's text inside or something. just print it
    (emit-element "hello")
    ;;we want a map with a tag
    (emit-element {:tag :hello})
    ;;strings works as well (since we're using the "name" function)
    (emit-element {:tag "hello"})
    ;;:attrs is for all the attributes
    (emit-element {:tag :hello :attrs {:place "world"}})
    <hello place='world'/>
    ;;:content is for all the children in the element
    (emit-element {:tag :parent :attrs {:id "22" :name "fritz"} :content [
                    {:tag :child :attrs {:id "56"}} 
                    {:tag :child :attrs {:id "57"}]}))
    <parent id='22' name='fritz'>
    <child id='56'/>
    <child id='57'/>
    ;;also, look out for not supplying :content with another xml-ish map:
    (emit-element {:tag :hello :content "world"})
    java.lang.NullPointerException (NO_SOURCE_FILE:0)
    ;;it expects a {:tag :something}
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/xml.clj:95 top

(defn emit-element [e]
  (if (instance? String e)
    (println e)
      (print (str "<" (name (:tag e))))
      (when (:attrs e)
	(doseq [attr (:attrs e)]
	  (print (str " " (name (key attr)) "='" (val attr)"'"))))
      (if (:content e)
	  (println ">")
	  (doseq [c (:content e)]
	    (emit-element c))
	  (println (str "")))
	(println "/>")))))
Vars in clojure.xml/emit-element:
Used in 0 other vars

Comments top

1 comment(s) for emit-element.

you can catch the printed code with the macro

if you want to catch it in variables.

Log in to add a comment.