문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top