score:1

Play is very silly in handling XML. M.b. not many used it with? Anyway, here is writeable that form proper xml document, also trims spaces. Just import it in controller.

 object Implicits {    
    implicit def writeableOf_Node(implicit codec: Codec): Writeable[NodeSeq] = {
      Logger.debug(s"Codec: $codec")
      Writeable{ xml => {
        val stringWriter = new StringWriter(1024)
        val node = scala.xml.Utility.trim(xml(0))
        scala.xml.XML.write(stringWriter, node, codec.charset, xmlDecl = true, null)
        val out = codec.encode(stringWriter.toString)
        stringWriter.close()
        out
      }}
    }
    }

score:3

OK is a Status, which uses a Writable typeclass to encode the content to bytes (see Status.apply). Looking at the source here, the Writable for xml nodes just calls toString. Basically, instead of a document with an xml declaration, you are just getting a string representation of the root node.

To get the decl, you need to call XML.write() with a java.io.Writer and xmlDecl = true, as well as the other parameters. (I was able to supply a null for the DocType, instead of inventing one).

You could write to a StringWriter and then send that, but I think the most efficient way (especially if your document is large) would be to create an implicit Writable for xml.Node to override the builtin one.


Related Query

More Query from same tag