我正在尝试在说明字段中创建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]]>data</description>
我做错了什么?!
答案 0 :(得分:6)
序列]]>
终止CDATA部分,因此不能出现在CDATA部分中。
您的XML库正在通过放弃CDATA部分并使用实体来恢复具有特殊含义的字符来恢复。
由于<foo><![CDATA[Hello, world>]]></foo>
和<foo>Hello, world></foo>
是等价的,所以这不是问题(除非有人试图用不是XML解析器的工具解析生成的XML,这种方式是疯狂的)。
答案 1 :(得分:5)
您应该指定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>
答案 2 :(得分:2)
使用以下方法:
CDATASection cdata = document.createCDATASection("");
答案 3 :(得分:0)