Created
March 30, 2011 22:55
-
-
Save sritchie/895482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns nakkaya.ppxml | |
(:import [java.io StringReader StringWriter] | |
[javax.xml.transform TransformerFactory OutputKeys] | |
[javax.xml.transform.stream StreamSource StreamResult])) | |
(defn ppxml | |
"Accepts an XML string with no newline formatting and returns the | |
same XML with pretty-print formatting, as described by Nurullah Akaya | |
in [this post](http://goo.gl/Y9OVO)." | |
[xml-str] | |
(let [in (StreamSource. (StringReader. xml-str)) | |
out (StreamResult. (StringWriter.)) | |
transformer (.newTransformer | |
(TransformerFactory/newInstance))] | |
(doseq [[prop val] {OutputKeys/INDENT "yes" | |
OutputKeys/METHOD "xml" | |
"{http://xml.apache.org/xslt}indent-amount" "2"}] | |
(.setOutputProperty transformer prop val)) | |
(.transform transformer in out) | |
(str (.getWriter out)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment