views:

37

answers:

2

Trying o create CDATA section withing description field, code is pretty simple in resulting XML CDATA section does not appear!!

Node de = document.createElement("description");
de.appendChild(document.createCDATASection(reportData.getIssue().getDescription() + "more]]>data"));
e.appendChild(de);

In result XML getting:

<description>Room #1128 has AD issues.more]]&gt;data</description>

What I'm doing wrong?!

A: 

You can't write a > in the XML Data.
It's being escaped into &gt; (greater than)

Notice that the Greater Than sign will mess up your </description> tag because its the beginning of an end tag.

You can read about it here (among other places)

Itsik
I think my case it specific implementaion, my point is if I asked to create CDATA section, why it is not in resulting XML?
voipsecuritydigest.com
+3  A: 

The sequence ]]> terminates a CDATA section and thus cannot appear within a CDATA section.

Your XML library is recovering by ditching the CDATA section and using entities for characters that would have special meaning.

Since <foo><![CDATA[Hello, world>]]></foo> and <foo>Hello, world&gt;</foo> are equivalent, this isn't a problem (unless someone tries to parse the resulting XML with a tool that isn't an XML parser, which way lies madness).

David Dorward
So you saying DOM4j is smart enough to figure out that CDATA section not needed in this case?
voipsecuritydigest.com
Not so much "not needed" as "invalid".
David Dorward
I wouldn't call it "invalid". If the string inside CDATA contains a "]]>" a proper CDATA implementation should divide the string into two CDATA sections.
jasso
Why I dont see any CDATA section? What would be resulting XML if my code will be: de.appendChild(document.createCDATASection(reportData.getIssue().getDescription()));
voipsecuritydigest.com
As my answer says, you don't see a CDATA section because you can't store the data you have using a CDATA section.
David Dorward