Search code examples
xmlscalapretty-print

How to prevent Scala's pretty printer from removing CDATA?


I've used Scala xml PrettyPrinter library to format xml files. It removes the tags content that has CDATA section. How can I prevent PrettyPrinter from removing CDATA?

This is the code :

val printer = new scala.xml.PrettyPrinter(80, 2)
printer.format(XML.loadString(input))

It converts <textFieldExpression><![CDATA[something]]></textFieldExpression> to <textFieldExpression>something</textFieldExpression>


Solution

  • I don't think that it's the PrettyPrinter that removes the CDATA, but the XML.loadString(...). If you try to create an XML like this and print it:

    val input = <textFieldExpression>{new xml.PCData("something")}</textFieldExpression>
    val printer = new scala.xml.PrettyPrinter(80, 2)
    printer.format(XML.loadString(input.toString))
    // => <textFieldExpression>something</textFieldExpression>
    printer.format(input)
    // => <textFieldExpression><![CDATA[something]]></textFieldExpression>