Parse the following document into a DOM tree, and use org.apache.xml.serialize.XMLSerializer to serialize it. Document: <?xml version="1.0" encoding="UTF-8"?> <top><![CDATA[]]]]><![CDATA[>]]> </top> Result: <?xml version="1.0" encoding="UTF-8"?> <top><![CDATA[]]>]]> </top> In the second line of the result: <![CDATA[ is the CDATA header ]] is the content of the first CDATA section > is the content of the second CDATA section ]]> is the CDATA terminator The problem is XMLSerializer concatentating CDATA sections without checking if that produces "]]>".
Created attachment 1472 [details] Patch to XMLSerializer.java to address this problem
Actually, the patch is to BaseMarkupSerializer.java