Question: 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. 

**Answer:**

It is not possible, otherwise you could create invalid XML documents.

Answer: 
If you want the XML to be included AS XML and not as character data, then it has to be parsed at some point. If you don't want to manually do the parsing yourself, you have two alternatives: 

(1) Use external parsed entities -- in this case the external file will be pulled in and parsed by the XML parser. When the output is again serialized, it will include the contents of the external file. 

[ See <http://www.javacommerce.com/displaypage.jsp?name=entities.sql&id=18238> ] 

(2) Use Xinclude -- in that case the file has to be run thru an xinclude processor which will merge the xinclude references into the output. Most xslt processors, as well as xmllint will also do xinclude with an appropriate option. 

[ See: <http://www.xml.com/pub/a/2002/07/31/xinclude.html> ] 

( XSLT can also be used to merge documents without using the XInclude syntax. XInclude just provides a standard syntax )
I don't see what the problem is with parsing the large block of XML you want to insert into your output. Use a StAX parser to parse it, and just write code to forward all of the events to your existing serializer (variable "xtw").