Creation of CDATA section is confusing

2019-04-10 21:06发布

问题:

I am trying to create CDATA section within the description field, but failing. The code is pretty simple, but in the resulting XML a CDATA section does not appear!!

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

In the result XML, I'm getting:

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

What am I doing wrong?!

回答1:

The sequence ]]> terminates a CDATA section and thus cannot appear within a CDATA section.

Your XML library is recovering by ditching the CDATA section and using entities for characters that would have special meaning.

Since <foo><![CDATA[Hello, world>]]></foo> and <foo>Hello, world&gt;</foo> are equivalent, this isn't a problem (unless someone tries to parse the resulting XML with a tool that isn't an XML parser, which way lies madness).



回答2:

You should specify CDATA section element(s).

You can do it like this;

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

if you want to specify more than one CDATA section element use white space as delimiter.

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

Full Code

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);

Result will be like this;

<?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>

If we want to apply your exact sample (with your data + "]]");

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

Then result will be like this;

    <?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>


回答3:

Use the below method:

CDATASection cdata = document.createCDATASection("");


回答4:

You can't write a > in the XML Data.
It's being escaped into &gt; (greater than)

Notice that the Greater Than sign will mess up your </description> tag because its the beginning of an end tag.

You can read about it here (among other places)



标签: java xml cdata