4

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

1 Answer 1

4

You need to write:

import scala.xml.EntityRef
...
val myXml = <body>Hello{EntityRef("#8198")}World</body>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This works, with a minor change: EntityRef("#8198")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.