Xerces setTextContent 方法去除换行符/回车符。如何预防?
我正在使用 Xerces (Java) 生成 XML 文档。我有一大块文本(包括回车符和换行符),我希望将其精确复制到 XML 文档中作为元素的文本内容。例如:
<element>This is some text
here is some more
that's all folks</element>
但是,每当我尝试使用:
element.setTextContent(myBlockOfText)
所有新行都被替换为单个空格字符。
如何使用 Xerces 将新行保留在该文本块中? (尝试过 CDATA,但它实际上仅用于在 XML 本身内引用 XML(左尖括号等))。
I'm using Xerces (Java) to generate XML documents. I have a large block of text (including carriage returns and new lines) that I would like to replicate exactly in my XML document as the text content of an element. For example:
<element>This is some text
here is some more
that's all folks</element>
However, whenever I try use:
element.setTextContent(myBlockOfText)
All the new lines are replaced with single space characters.
How can I keep the new lines within this block of text using Xerces? (Tried CDATA, but it's really only for quoting XML (left angle brackets etc) within XML itself).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对文档有控制权吗?如果是这样,您可以尝试 xml:space 属性。请参阅http://www.w3.org/TR/xml/#sec-空白 和 http://codeidol.com/java/java-xml-for-web/Generate-and-Serializing-XML-Documents/Handling-Whitespace/
Do you have control of the document? If so, you might try the xml:space attribute. See http://www.w3.org/TR/xml/#sec-white-space and http://codeidol.com/java/java-xml-for-web/Generating-and-Serializing-XML-Documents/Handling-Whitespace/