You're viewing version 1.2.0 of emit-element. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

emit-element

clojure.xml

  • (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")
    hello
    
    ;;we want a map with a tag
    (emit-element {:tag :hello})
    <hello/>
    
    ;;strings works as well (since we're using the "name" function)
    (emit-element {:tag "hello"})
    <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'/>
    </parent>
    
    ;;also, look out for not supplying :content with another xml-ish map:
    (emit-element {:tag :hello :content "world"})
    <hello>
    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)
    (do
      (print (str "<" (name (:tag e))))
      (when (:attrs e)
	(doseq [attr (:attrs e)]
	  (print (str " " (name (key attr)) "='" (val attr)"'"))))
      (if (:content e)
	(do
	  (println ">")
	  (doseq [c (:content e)]
	    (emit-element c))
	  (println (str "")))
	(println "/>")))))
Vars in clojure.xml/emit-element: defn doseq instance? key name print println str val when
Used in 0 other vars

Comments top

No comments for emit-element. Log in to add a comment.