简体   繁体   English

将Java DOM文档序列化为XML:添加CData元素

[英]Serializing Java DOM Document to XML: Add CData Elements

I am constructing an XML DOM Document with a SAX parser. 我正在使用SAX解析器构造XML DOM文档。 I have written methods to handle the startCDATA and endCDATA methods and in the endCDATA method I construct a new CDATA section like this: 我已经编写了处理startCDATAendCDATA方法的方法,并在endCDATA方法中构造了一个新的CDATA部分,如下所示:

public void onEndCData() {
    xmlStructure.cData = false;
    Document document = xmlStructure.xmlResult.document;
    Element element = (Element) xmlStructure.xmlResult.stack.peek();
    CDATASection section = document.createCDATASection(xmlStructure.stack.peek().characters);
    element.appendChild(section);
}

When I serialize this to an XML file I use the following line to configure the transformer: 当我将此序列化为XML文件时,我使用以下行来配置转换器:

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "con:setting");

Never the less no <![CDATA[ tags appear in my XML file and instead all backets are escaped to &gt; 永远不会没有<![CDATA[标记]出现在我的XML文件中,而所有backets都转义为&gt; and &lt; &lt; , this is no problem for other tools but it is a problem for humans who need to read the file as well. ,对于其他工具来说这没问题,但对于需要读取文件的人来说也是一个问题。 I am positive that the "con:setting" tag is the right one. 我很肯定“ con:setting”标签是正确的。 So is there maybe a problem with the namespace prefix? 那么名称空间前缀可能有问题吗?

Also this question indicates that it is not possible to omit the CDATA_SECTION_ELEMENTS property and generally serialize all CDATA nodes without escaping the data. 这个问题还表明,不可能省略CDATA_SECTION_ELEMENTS属性,并且通常在不转义数据的情况下序列化所有CDATA节点。 Is that information correct, or are there maybe other methods that the author of the answer was not aware of? 该信息是否正确,或者答案的作者可能还不知道其他方法?

Update: It seems I had a mistake in my code. 更新:似乎我的代码有误。 When using the document.createCDATASection() function, and then serializing the code with the Transformer it DOES output CDATA tags, even without the use of the CDATA_SECTION_ELEMENTS property in the transformer. 当使用document.createCDATASection()函数,然后用序列化的输出CDATA标签变压器的代码,即使不使用的CDATA_SECTION_ELEMENTS在变压器特性。

It looks like you have a namespace-aware DOM. 看起来您有一个支持名称空间的DOM。 The docs say you need to provide the Qualified Name Representation of the element: 文档说您需要提供元素的合格名称表示

private static String qualifiedNameRepresentation(Element e) {
  String ns = e.getNamespaceURI();
  String local = e.getLocalName();
  return (ns == null) ? local : '{' + ns + '}' + local;
}

So the value of the property will be of the form {http://your.conn.namespace}setting . 因此,该属性的值将采用{http://your.conn.namespace}setting的形式。

In this line 在这条线

transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "con:setting");

try replacing "con:setting" with "{http://con.namespace/}setting" 尝试将"con:setting"替换为"{http://con.namespace/}setting"

using the appropriate namespace 使用适当的名称空间

Instead of using a no-op Transformer to serialize your DOM tree you could try using the DOM-native "load and save" mechanism , which should preserve the CDATASection nodes from the DOM tree and write them as CDATA sections in the resulting XML. 您可以尝试使用DOM原生的“加载和保存”机制 ,而不是使用no-op Transformer来序列化DOM树,该机制应保留DOM树中的CDATASection节点,并将其作为CDATA节写入到所得XML中。

DOMImplementationLS ls = (DOMImplementationLS)document.getImplementation();
LSOutput output = ls.createLSOutput();
LSSerializer ser = ls.createLSSerializer();
try (FileOutputStream outStream = new FileOutputStream(...)) {
  output.setByteStream(outStream);
  output.setEncoding("UTF-8");
  ser.write(document, output);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM