[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


JDOM - Wrapping text/xml with CDATA

Posted on 2012-08-16
Medium Priority
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
  • 4
  • 3
LVL 86

Expert Comment

ID: 38301778
however the input string could contain ']]>',

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

Author Comment

ID: 38301823
ha !...Yes a coincidence...

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

Accepted Solution

CEHJ earned 2000 total points
ID: 38301994
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 38302056
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

ID: 38302087

Author Comment

ID: 38302148
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

ID: 38464541

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 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