How do I write unescaped XML outside of a CDATA

Posted by kazanaki on Stack Overflow See other posts from Stack Overflow or by kazanaki
Published on 2010-06-08T10:15:41Z Indexed on 2010/06/08 10:32 UTC
Read the original article Hit count: 312

Filed under:
|
|
|
|

Hello

I am trying to write XML data using Stax where the content itself is HTML

If I try

xtw.writeStartElement("contents");
xtw.writeCharacters("<b>here</b>");
xtw.writeEndElement();

I get this

<contents>&lt;b&gt;here&lt;/b&gt;</contents>

Then I notice the CDATA method and change my code to:

xtw.writeStartElement("contents");
xtw.writeCData("<b>here</b>");
xtw.writeEndElement();

and this time the result is

<contents><![CDATA[<b>here</b>]]></contents>

which is still not good. What I really want is

<contents><b>here</b></contents>

So is there an XML API/Library that allows me to write raw text without being in a CDATA section? So far I have looked at Stax and JDom and they do not seem to offer this.

In the end I might resort to good old StringBuilder but this would not be elegant.

Update

I agree mostly with the answers so far. However instead of <b>here</b> I could have a 1MB HTML document that I want to embed in a bigger XML document. What you suggest means that I have to parse this HTML document in order to understand its structure. I would like to avoid this if possible.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml