質問

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