I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?
Using scala:
import xml._
val xml = XML.loadString("hello ")
val formatted = new PrettyPrinter(150, 2).format(xml)
println(formatted)
You can do this in Java too, if you depend on the scala-library.jar. It looks like this:
import scala.xml.*;
public class FormatXML {
public static void main(String[] args) {
String unformattedXml = "hello ";
PrettyPrinter pp = new PrettyPrinter(150, 3);
String formatted = pp.format(XML.loadString(unformattedXml), TopScope$.MODULE$);
System.out.println(formatted);
}
}
The PrettyPrinter
object is constructed with two ints, the first being max line length and the second being the indentation step.