XML format Exception-cdata nesting __xml

Source: Internet
Author: User
Tags cdata

Today's code parsing an XML times error, after checking to find that there are nested CDATA flags, resulting in changes in the XML structure, can not be converted to XML objects, Baidu, to find the following solution, the test under the to share.

Http://hi.baidu.com/yin_xiaogang/blog/item/f0f4a4ce3ef0c50592457e78.html

All content within CDATA is ignored by the parser. A CDATA part with "<! The [cdata[] mark begins with the "]]>" tag and ends. But keep in mind that CDATA cannot be nested. But now, there is someone who wants to implement CDATA nesting.

For example, add the following XML to an element in another XML:

<?xml version= "1.0" encoding= "UTF-8"?>
<System><id>library</id>
<name>library management</name>
<description><! [Cdata[this is a sample library management subsystem for Signet.]] ></description>
<Categories>
<System/>

Escapes the end of the inner CDATA. Then it becomes "<!. "Cdata[" and "]]>amp;", this is not good, the teacher said, the recipient of the XML how to know that you added an escape inside.

So, you can break the CDATA end of "]]>", divide it into two cdata, and then call the Java API to read the element's data, and the API actually reads two CDATA blocks and then synthesizes one.

Then, treat the above XML as a string and replace "]]>" with "]]]]><! [Cdata[, please remember, do not include spaces. In this way, the result should be

<?xml version= "1.0" encoding= "UTF-8"?>
<Messages>
<message type= "Data" >
<code>1199242678515-176590595</code>
<date>2008/01/02</date>
<time>10:57:58</time>
<content><! [Cdata[<?xml version= "1.0" encoding= "UTF-8"?>
<System><id>library</id>
<name>library management</name>
<description><! [Cdata[this is a sample library management subsystem for Signet.]]] ><! [cdata[></description>
<Categories>
<System/>]]></content>
</Message>

</Messages>

Thus, the data obtained using the object Org.dom4j.Element.getData () method is the original XML fragment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.