For example, the prxml function prints XML to *out*. I would like to instead capture this output as a String. Here is the typical usage from a REPL:
user> (prxml [:p "Test"])
<p>Test</p>nil
I'd instead like to do:
(def xml (capture-out (prxml [:p "Test"])))
I made up the capture-out function, but I suspect something like it exists, only I'm having trouble finding it in the API or mailing list.
I just discovered the with-out-str from this great blog post detailing XML processing in Clojure.
So the correct implementation of my example is:
(def xml (with-out-str (prxml [:p "Test"])))
More generally, if you look at the source for with-out-str
you can see how to dynamically bind *out*
to any stream using binding
. This works for dynamically setting the value of any existing var.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With