-->

Xerces setTextContent method strips new lines / ca

2019-09-03 04:13发布

问题:

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/