Question

I have an XMl containing a CDATA section containing a nested CDATA section. How is the unmarshalling supposed to be done in this case. Is this possible in Java? Thanks in advance.

Was it helpful?

Solution

No, it's not possible to unmarshal such a document, because the document is invalid. For example:

<?xml version="1.0" ?>
<foo>
  <bar><![CDATA[This should contain a nested cdata, which <![CDATA[Contains another CDATA]]>]]></bar>
</foo>

Fails to parse because the character sequence ]]> can only be used to mark the end of a CDATA section. In a nutshell, you can't nest CDATA sections. You'll have to figure out another approach.

See the wikipedia article, and this question and answers for more information.

OTHER TIPS

JAXB framework is what you are looking for. JAXB stands for Java Architecture for XML Binding.

JAXB object has the capability of Marshaling and UnMarshaling. 

It is not complex. Go for it :)

In context of your question,

You can do it by having multilevel class structure. For Eg:

Suppose you have two classes :

class A
{
  (@xmlName : CDATA1)
  B obj;
}

class B
{
  (@xmlName : CDATA2)
  String string;
}

Here when you unmarshal in context of object of class A.

The inner CDATA would land up in the string member of obj.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top