PrettyPrinter for Serializing Xml With Scales

26 views
Skip to first unread message

Marko D

unread,
Feb 6, 2013, 1:35:33 PM2/6/13
to scale...@googlegroups.com
Is there something like PrettyPrinter for serializing Xml With Scales ?
I know that I can get a scala.xml.Node and use scala.xml.PrettyPrinter, but I was wondering if there is something that I can use with scales.xml.WriteTo.

Mark

Chris Twiner

unread,
Feb 7, 2013, 3:06:49 AM2/7/13
to scale...@googlegroups.com
Hi Mark,

Scales works very nicely with TrAX, so the standard approach works here too:

import scales.xml._
import ScalesXml._

val elem = Elem("trax"l)
val doc = Doc(<(elem) /( <(elem) ~> "value" ))

import javax.xml.transform._
val trax = TransformerFactory.
newInstance.newTransformer

// pretty printing
trax.setOutputProperty(OutputKeys.INDENT, "yes")
trax.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")

val wr = new java.io.StringWriter()
val str = new stream.StreamResult(wr)
trax.transform(doc, str)

println(wr.toString)

its possible on a non apache dom that other properties must be used.
For jdk7 the above runs directly via StAX and doesn't need
intermediate conversions, on 1.6 it however does.

Cheers,
Chris
> --
> You received this message because you are subscribed to the Google Groups
> "scales-xml" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scales-xml+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Marko D

unread,
Feb 7, 2013, 2:38:26 PM2/7/13
to scale...@googlegroups.com
Thanks Chris

Chris Twiner

unread,
Feb 8, 2013, 5:47:19 PM2/8/13
to scale...@googlegroups.com
More than welcome:

https://gist.github.com/chris-twiner/4742571

contains an indenting SerializerFactory that wraps other actual
serializers. I may aim to put that into the actual release. It
doesn't do normalisation of text but its not bad for an hour or so of
fiddling :)

On Thu, Feb 7, 2013 at 8:38 PM, Marko D <markod...@gmail.com> wrote:
> Thanks Chris
Reply all
Reply to author
Forward
0 new messages