【问题标题】:Creation of CDATA section is confusingCDATA 部分的创建令人困惑
【发布时间】:2011-04-20 23:21:27
【问题描述】:

我正在尝试在描述字段中创建 CDATA 部分,但失败了。代码很简单,但是在生成的 XML 中没有出现 CDATA 部分!!

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

在结果 XML 中,我得到:

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

我做错了什么?!

【问题讨论】:

  • 为什么在文本中放置]]&gt;createCDATASection 肯定也会生成吗?
  • 没关系!我想在生成的 XML 中查看 CDATA,它在哪里?
  • 问题是如果你把“>”在 CDATA 部分中,您将获得“>”当它被解释回来时,不是“>”。您所看到的几乎是保留“>”的唯一方法...

标签: java xml cdata


【解决方案1】:

序列]]&gt; 终止一个CDATA 部分,因此不能出现在CDATA 部分中。

您的 XML 库正在通过放弃 CDATA 部分并为具有特殊含义的字符使用实体来恢复。

由于&lt;foo&gt;&lt;![CDATA[Hello, world&gt;]]&gt;&lt;/foo&gt;&lt;foo&gt;Hello, world&amp;gt;&lt;/foo&gt; 是等价的,所以这不是问题(除非有人尝试使用不是 XML 解析器的工具来解析生成的 XML,否则这简直是疯了)。

【讨论】:

  • 所以你说 DOM4j 足够聪明,可以找出在这种情况下不需要 CDATA 部分?
  • 与其说是“不需要”,不如说是“无效”。
  • 我不会称之为“无效”。如果 CDATA 中的字符串包含“]]>”,则正确的 CDATA 实现应该将字符串分成两个 CDATA 部分。
  • 为什么我没有看到任何 CDATA 部分?如果我的代码是:de.appendChild(document.createCDATASection(reportData.getIssue().getDescription()));
  • 正如我的回答所说,您看不到 CDATA 部分,因为您无法使用 CDATA 部分存储您拥有的数据。
【解决方案2】:

您应该指定 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>

【讨论】:

    【解决方案3】:

    使用以下方法:

    CDATASection cdata = document.createCDATASection("");
    

    【讨论】:

      【解决方案4】:

      您不能在 XML 数据中写入 &amp;gt;
      它被转义到&amp;gt;(大于)

      请注意,大于符号会弄乱你的&lt;/description&gt;标签,因为它是结束标签的开始。

      你可以阅读它here(以及其他地方)

      【讨论】:

      • 我认为我的情况是具体实现,我的意思是如果我要求创建 CDATA 部分,为什么它不在生成的 XML 中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2022-01-21
      • 2022-01-17
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多