将 CDataElements 添加到 DOMConfiguration
我正在使用 LSSerializer
生成 XML。问题是,使用 OutputFormat
我可以设置一个 CData
数组,但我无法使用从LSSerializer
。
这是旧版本:
OutputFormat of = new OutputFormat();
String[] arrayOfCDATAHandledElements = { "^Tag1", "^Tag2" };
of.setCDataElements(arrayOfCDATAHandledElements);
新版本:
LSSerializer domWriter = impls.createLSSerializer();
DOMConfiguration domConfig = domWriter.getDomConfig();
String[] arrayOfCDATAHandledElements = { "^Tag1", "^Tag2" }; // how would one add it?
domConfig.setParameter("cdata-sections", Boolean.TRUE);
我希望为 Tag1 和 Tag1 添加 CDATA。我的 XML 中的 Tag2。
I'm generating XML using LSSerializer
. The problem is that with OutputFormat
I can set an array of CData
, but I can't do it with the DOMConfiguration
that I get from the LSSerializer
.
This is the old version :
OutputFormat of = new OutputFormat();
String[] arrayOfCDATAHandledElements = { "^Tag1", "^Tag2" };
of.setCDataElements(arrayOfCDATAHandledElements);
And the new version :
LSSerializer domWriter = impls.createLSSerializer();
DOMConfiguration domConfig = domWriter.getDomConfig();
String[] arrayOfCDATAHandledElements = { "^Tag1", "^Tag2" }; // how would one add it?
domConfig.setParameter("cdata-sections", Boolean.TRUE);
I want the CDATA to be added for Tag1 & Tag2 in my XML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我可以看到这个问题是 2012 年提出的,但我最近在尝试修复一些非常旧的代码时遇到了同样的问题。据我所知,当您构建文档时,您应该确保需要具有 CDATA 标记的任何节点都按如下方式构建:
这与 OutputFormat 通过指定需要哪些元素(按名称)来处理 CDATA 的方式形成鲜明对比 。格式为 CDATA。
While I can see this question is from 2012, I ran into this same issue while recently trying to fix some very old code. From what I can tell, when you build your document, you should be making sure that any nodes that need to have CDATA markers are built as follows:
This is in contrast to how OutputFormat handled CDATA by specifying which elements (by name) needed to be formatted as CDATA.