1.2.0 permalink Arrow_down_16x16
  • (emit e & {:as opts})

1 Example top

  • (require 'clojure.contrib.lazy-xml)
    
    (clojure.contrib.lazy-xml/emit  
      {:tag :div 
       :attrs {:style "border:1px solid red"} 
       :content [{:tag :p :content ["This is a div"]}]}
      :indent 4)
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/lazy_xml.clj:139 top

(defn emit
  [e & {:as opts}]
  (let [content-handler (atom nil)
        trans (-> (javax.xml.transform.TransformerFactory/newInstance)
                .newTransformer)]

    (when (:indent opts)
      (.setOutputProperty trans "indent" "yes")
      (.setOutputProperty trans "{http://xml.apache.org/xslt}indent-amount"
                          (str (:indent opts))))

    (when (contains? opts :xml-declaration)
      (.setOutputProperty trans "omit-xml-declaration" 
                          (if (:xml-declaration opts) "no" "yes")))

    (when (:encoding opts)
      (.setOutputProperty trans "encoding" (:encoding opts)))

    (.transform
      trans
      (javax.xml.transform.sax.SAXSource.
        (reify org.xml.sax.XMLReader
          (getContentHandler [_] @content-handler)
          (setDTDHandler [_ handler])
          (setFeature [_ name value])
          (setProperty [_ name value])
          (setContentHandler [_ ch] (reset! content-handler ch))
          (^void parse [_ ^org.xml.sax.InputSource _]
            (when @content-handler
              (.startDocument @content-handler)
              (emit-element e @content-handler)
              (.endDocument @content-handler))))
        (org.xml.sax.InputSource.))
      (javax.xml.transform.stream.StreamResult. *out*))))
Vars in clojure.contrib.lazy-xml/emit: *out* contains? defn let reify reset! str when deref
Used in 0 other vars

Comments top

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