How to handle XML character in scala?

I am trying to generate some XML on the fly in Scala.

I want to use a numeric character reference inside XML and write the resulting XML to the output stream.

Example:

val myXml = <body>Hello&#8198;World</body> val writer = new java.io.FileWriter("test") scala.xml.XML.write(writer, myXml, "utf-8", false, null) 

8198 is unicode for a tiny space character.

After executing the above fragment, the contents of the file "test"

 <body>Hello World</body> 

Instead, I expect

 <body>Hello&#8198;World</body> 

Edit : Learned how to avoid XML in SO

+4
source share
1 answer

You need to write:

 import scala.xml.EntityRef ... val myXml = <body>Hello{EntityRef("#8198")}World</body> 
+4
source

All Articles