java CDATA 部分的创建令人困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3858290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creation of CDATA section is confusing
提问by xchg.ca
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!!
我试图在描述字段中创建 CDATA 部分,但失败了。代码非常简单,但是在生成的 XML 中没有出现 CDATA 部分!!
Node de = document.createElement("description");
de.appendChild(document.createCDATASection(reportData.getIssue().getDescription() + "more]]>data"));
e.appendChild(de);
In the result XML, I'm getting:
在结果 XML 中,我得到:
<description>Room #1128 has AD issues.more]]>data</description>
What am I doing wrong?!
我究竟做错了什么?!
回答by Quentin
The sequence ]]>
terminates a CDATA section and thus cannot appear within a CDATA section.
该序列]]>
终止 CDATA 部分,因此不能出现在 CDATA 部分中。
Your XML library is recovering by ditching the CDATA section and using entities for characters that would have special meaning.
您的 XML 库正在通过放弃 CDATA 部分并使用具有特殊含义的字符的实体来恢复。
Since <foo><![CDATA[Hello, world>]]></foo>
and <foo>Hello, world></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).
因为<foo><![CDATA[Hello, world>]]></foo>
和<foo>Hello, world></foo>
是等价的,所以这不是问题(除非有人尝试使用不是 XML 解析器的工具来解析生成的 XML,这种方式很疯狂)。
回答by azizunsal
You should specify CDATA section element(s).
您应该指定 CDATA 部分元素。
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.
如果要指定多个 CDATA 部分元素,请使用空格作为分隔符。
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>
回答by Aftab
Use the below method:
使用以下方法:
CDATASection cdata = document.createCDATASection("");
回答by Itsik
You can't write a >
in the XML Data.
It's being escaped into >
(greater than)
您不能>
在 XML 数据中写入 a 。
它被转义为>
(大于)
Notice that the Greater Thansign will mess up your </description>
tag because its the beginning of an end tag.
请注意,大于号会弄乱您的</description>
标签,因为它是结束标签的开始。
You can read about it here(among other places)
你可以在这里阅读它(以及其他地方)