Creating a CDATA Section Confuses

I am trying to create a CDATA section in the description field, but it fails. The code is pretty simple, but CDATA doesn’t appear in the resulting XML section !!

Node de = document.createElement("description");
de.appendChild(document.createCDATASection(reportData.getIssue().getDescription() + "more]]>data"));
e.appendChild(de);

As a result of XML, I get:

<description>Room #1128 has AD issues.more]]&gt;data</description>

What am I doing wrong?!

+3
source share
4 answers

The sequence ]]>completes the CDATA section and therefore cannot appear in the CDATA section.

Your XML library is being restored by tearing off the CDATA section and using entities for characters that make special sense.

<foo><![CDATA[Hello, world>]]></foo> <foo>Hello, world&gt;</foo> , ( - XML , XML, ).

+6

() CDATA.

:

 transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "tagName");

CDATA, .

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "tagName1 tagName2");

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("catalog");
doc.appendChild(rootElement);

Element description = doc.createElement("description");
description.appendChild(doc.createCDATASection("/&(*/**SOME STRANGE DESCRIPTION**ĞĞÜ656*9/*9^+%3ÜĞPÜ"));
rootElement.appendChild(description);

Element books = doc.createElement("books");
rootElement.appendChild(books);

Element book = doc.createElement("book");
books.appendChild(book);

Element author = doc.createElement("author");
author.appendChild(doc.createCDATASection("&/(&/(QNzxB5yiBibGj2MM ÇÖÇÇ"));
book.appendChild(author);

Element price = doc.createElement("price");
price.appendChild(doc.createTextNode("50.5"));
book.appendChild(price);

Element title = doc.createElement("title");
title.appendChild(doc.createTextNode("my book title"));
book.appendChild(title);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "description author descr");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(System.out);

transformer.transform(source, result);

:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <description><![CDATA[/&(*/**SOME STRANGE DESCRIPTION**ĞĞÜ656*9/*9^+%3ÜĞPÜ]]></description>
   <books>
      <book>
         <author><![CDATA[&/(&/(QNzxB5yiBibGj2MM ÇÖÇÇ]]></author>
         <price>50.5</price>
         <title>my book title</title>
      </book>
   </books>
</catalog>

( + "]]" );

String someInfo = "example-info";
Element dscr = doc.createElement("descr");
dscr.appendChild(doc.createCDATASection(someInfo + "more]]>data"));
book.appendChild(dscr);

:

    <?xml version="1.0" encoding="UTF-8"?>
    <catalog>
       <description><![CDATA[/&(*/**SOME STRANGE DESCRIPTION**ĞĞÜ656*9/*9^+%3ÜĞPÜ]]></description>
       <books>
          <book>
             <author><![CDATA[&/(&/(QNzxB5yiBibGj2MM ÇÖÇÇ]]></author>
             <price>50.5</price>
             <title>my book title</title>
             <descr><![CDATA[example-infomore]]]]><![CDATA[>data]]></descr>
          </book>
       </books>
    </catalog>
+4

:

CDATASection cdata = document.createCDATASection("");
+2

> XML-.
&gt; ()

, Greater Than </description>, .

( )

0

Source: https://habr.com/ru/post/1767936/


All Articles