I sadly have a requirement to generate some messy XML.
The main document must contain an embedded XML document. However, the embedded document occurs within a CDATA section. The final result should look something like this:
<?xml version="1.0"?>
<foo>
<xml>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<bar>
</bar>
]]>
</xml>
</foo>
I am running into two problems:
First, anything that is output within the CDATA section is rendering as escaped (e.g., the greater than sign > becomes >)
Is there a way to disable the escaping within the CDATA section?
Second, I am unable to create the XML declaration. I receive the following exception when attempting to include the embedded XML document:
def serializeEmbedded(): Seq[Node] = {
<?xml version="1.0"?>
<bar>
</bar>
}
Exception in thread "main" java.lang.IllegalArgumentException: xml is reserved
at scala.xml.ProcInstr.<init>(ProcInstr.scala:25)
This is my first foray into Scala’s native XML processing.
Thank you,
Saish
The XML declaration is only really relevant for serialization, and you can’t specify it using Scala’s XML literal syntax (as you’ve discovered).
I’d suggest defining a helper function like this:
Now you can write, for example:
It’s not as elegant as it could be with a little more language support, but it works.