Skip to content

juanmirocks/Scala-XMLPrettyPrinter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XMLPrettyPrinter ✨ scala: 2.11, 2.12, 2.13

Advantages over scala.xml.PrettyPrinter:

  1. You can both pretty-format a String or pretty-write directly to a java File, OutputStream, or Writer.
  2. 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.

  1. Thread safe: you can have the same printer object used by different clients in parallel.

  2. Faster and consumes less memory when you write directly to your desired output, thus avoiding an in-between conversion to String.

Output Example

<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: &gt; &amp; ; &lt;pio&gt;*&lt;/pio&gt; &lt;!-- ? --&gt;</span>
  </notPre>
</notPre>

How to use

XMLPrettyPrinter is self-contained and has no dependencies other than the standard scala-xml library.

  1. Make sure you have scala-xml available in your project.
libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "1.2.0"
  1. Drop the single-file code into your project