Xerces setTextContent method strips new lines / carriage returns. How to prevent?

烈酒焚心 提交于 2019-12-12 00:33:38

问题


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).


回答1:


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/



来源:https://stackoverflow.com/questions/6551405/xerces-settextcontent-method-strips-new-lines-carriage-returns-how-to-prevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!