gpt4 book ai didi

java - CDATA 部分的创建令人困惑

转载 作者:数据小太阳 更新时间:2023-10-29 02:15:41 30 4
gpt4 key购买 nike

我试图在描述字段中创建 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>

我做错了什么?!

最佳答案

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

关于java - CDATA 部分的创建令人困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3858290/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com