Html inside XML. Should I use CDATA or encode the HTML [closed] Html inside XML. Should I use CDATA or encode the HTML [closed] xml xml

Html inside XML. Should I use CDATA or encode the HTML [closed]


The two options are almost exactly the same. Here are your two choices:

<html>This is <b>bold</b></html><html><![CDATA[This is <b>bold</b>]]></html>

In both cases, you have to check your string for special characters to be escaped. Lots of people pretend that CDATA strings don't need any escaping, but as you point out, you have to make sure that "]]>" doesn't slip in unescaped.

In both cases, the XML processor will return your string to you decoded.


CDATA is easier to read by eye while encoded content can have end of CDATA markers in it safely — but you don't have to care. Just use an XML library and stop worrying about it. Then all you have to say is "Put this text inside this element" and the library will either encode it or wrap it in CDATA markers.