JDOM - Wrapping text/xml with CDATA

Posted on 2012-08-16
Last Modified: 2012-10-04
I am trying to wrap a String with CDATA tags, however the input string could contain ']]>', if so then i get the following Exception :

The data "This is a]]> test" is not legal for a JDOM CDATA section: CDATA cannot internally contain a CDATA ending delimiter (]]>).

Is there an easy way I can get around this ? I want to ensure my input string is always wrapped in CDATA. I thought about replaing all occurances of ']]>' with say '£££' and then replacing then replacing the ']]>' afterwards ?

Any help would be gratefully received....thanks

Here is my code :

import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class Wrapper {

	public static void main(String args[]) throws Exception {

		// String pString = "This is a test";
		String pString = "This is a]]> test";

		Wrapper wrapper = new Wrapper();

		Document doc = wrapper.wrapWithCdata(pString);


	protected Document wrapWithCdata(String pString) throws Exception {

		Document doc = null;
		String xml = "<root></root>";

		try {
			doc = new SAXBuilder().build(new StringReader(xml));
			Element root = doc.getRootElement();
			root.addContent(new CDATA(pString));

			XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
			outputter.output(doc, System.out);

		} catch (Exception e) {

		return doc;

Open in new window

Question by:Molko
    LVL 86

    Expert Comment

    however the input string could contain ']]>',

    That's quite a coincidence ;) Why can it?

    Author Comment

    ha !...Yes a coincidence...

    Well the String is actually badly formed html/xml document....
    LVL 86

    Accepted Solution


    Author Comment

    Thanks for the link

    A CDATA section cannot contain the string "]]>" and therefore it is not possible for a CDATA section to contain nested CDATA sections.    That does not sound promising.

    This means that to encode "]]>" in the middle of a CDATA section, replace all occurrences of "]]>" with the following:


    Does that mean i would have 2 (or more) CDATA ?....
    LVL 86

    Expert Comment


    Author Comment

    hmmm. I'll need to have a think then, becuase I run some regExp over the Document (which is essentially the String wrapped in CDATA encased in <root><root> via an XSLT....I'm hoping the extra CDATA wont get in the way ?

    Given the code i showed above, whats the best way to issue the replace("]]>", "]]]]><![CDATA[>")  ?

    I've found it akwards issuing the replace() statement...I did intend to do something similar by replace ]]> with £££ but found I had to create the JDOM structure twice.


    Author Closing Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now