0

I am having a problem while parsing XML with nested CDATA section. The CDATA section is as below:

<![CDATA[*** some text

[ !  <![CDATA[some text]]>  ! ]

<![CDATA[some text]]>

]]>

When this type of data is appearing in an XML tag, it is giving an error while parsing the XML as there are two closing tags ]]>. Can anyone please suggest me what to do or what character should I escape to make this work? I am using Java1.8.

4
  • How are you parsing the XML? SAX, StAX, DOM, something else? Can you show us a minimal programm that generates this error? Commented Aug 10, 2018 at 8:00
  • Actually, some other application is parsing the XML and it is Oracle ESB. But even when I paste this in Notepad and try to beautify XML, it is showing that it is not a valid XML. It is working when I remove one ]]> end tag. Commented Aug 10, 2018 at 8:04
  • 1
    See en.wikipedia.org/wiki/CDATA#Nesting for an workaround. Commented Aug 10, 2018 at 8:16
  • @GyroGearless - Thanks, it worked! Commented Aug 10, 2018 at 9:44

1 Answer 1

1

The reason you are having trouble parsing this input is that it isn't XML. You need to find out what program is generating this non-XML and fix it.

If you are generating XML, then whenever you create a text node that might contain the sequence ]]> (whether or not this represents the end of a nested CDATA section) you need to escape it somehow. Many people take the path of least resistance and simply escape > as &gt; wherever it occurs, but technically this is needed only if the > is preceded by ]].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.