Advantages over scala.xml.PrettyPrinter:
- You can both pretty-format a String or pretty-write directly to a java File, OutputStream, or Writer.
- Pre-formatted elements: specify which nodes you want to be considered as pre-formatted. These are written exactly as they are read, with all the white spaces and new lines. All the other elements are completely stripped of leading & trailing whitespaces.
Consider for instance Polyglot Markup documents. You can write in XML with the same behavior as with the HTML <pre> tag. You could also have inlined <span>'s within a <p> without creating spurious break lines.
-
Thread safe: you can have the same printer object used by different clients in parallel.
-
Faster and consumes less memory when you write directly to your desired output, thus avoiding an in-between conversion to String.
<notPre> <!-- pre-formatting pretty-printing test (this comment should be aligned)--> <pre> .... </pre> <notPre> <span>child span</span> <!-- another comment --> <pre> !!! an <span>inlined span</span> doesn't get broken and can have<span> spaces!! </span><span>🙂</span></pre> <span>child span with specials: > & ; <pio>*</pio> <!-- ? --></span> </notPre> </notPre>XMLPrettyPrinter is self-contained and has no dependencies other than the standard scala-xml library.
- Make sure you have
scala-xmlavailable in your project.
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.2.0"- Drop the single-file code into your project