Scala Language XML Handling Beautify or Pretty-Print XML

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The PrettyPrinter utility will 'pretty print' XML documents. The following code snippet pretty prints unformatted xml:

import scala.xml.{PrettyPrinter, XML}
val xml = XML.loadString("<a>Alana<b><c>Beth</c><d>Catie</d></b></a>")
val formatted = new PrettyPrinter(150, 4).format(xml)
print(formatted)

This will output the content using a page width of 150 and an indentation constant of 4 white-space characters:

<a>
    Alana
    <b>
        <c>Beth</c>
        <d>Catie</d>
    </b>
</a>

You can use XML.loadFile("nameoffile.xml") to load xml from a file instead of from a string.



Got any Scala Language Question?