views:

113

answers:

1

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

I want to use a numeric character reference inside the XML and write out the resultant XML to an 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 the above snippet is run, the contents of the file "test" are

<body>Hello World</body>

What I am expecting instead is,

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

Edit: Learnt how to escape XML in SO

+3  A: 

You need to write:

import scala.xml.EntityRef
...
val myXml = <body>Hello{EntityRef("#8198")}World</body>
Walter Chang
Thanks. This works, with a minor change: EntityRef("#8198")
HRJ
@HRJ Thanks, corrected.
Walter Chang