如何在 XML 文件中添加换行符(换行符)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35504890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a newline (line break) in XML file?
提问by Christian
I have an XML file and I would like to make a new line in the text "Sample Text 123" like this
我有一个 XML 文件,我想像这样在文本“Sample Text 123”中创建一个新行
Sample
Text 123
I've tried already everything I mean 

\nbut nothing works:
我已经尝试了我的意思,

\n但没有任何效果:
<?xml version="1.0" encoding="UTF-8" ?>
<item>
<text>Address</text>
<data>
Sample 
 Text 123
</data>
</item>
回答by kjhughes
A newline(aka line breakor end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:
甲换行符(又名断线或结束线,EOL)是特殊字符或字符序列标记的文本行的末尾。使用的确切代码因操作系统而异:
LF: Unix
CR: Mac OS up to version 9
CR+LF: Windows, DOS
You can use 
for line feed (LF) or 
for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application. These can be added manually, as you show in your example, but are particularly convenient when needing to add newlines programmatically within a string:
您可以使用
换行符 (LF) 或
回车符 (CR),当将解析的文本交给应用程序时,XML 解析器会将其替换为相应的字符。这些可以手动添加,如您在示例中所示,但在需要以编程方式在字符串中添加换行符时特别方便:
- Common programming languages:
LF:"
"CR:"
"
- XSLT:
LF:<xsl:text>
</xsl:text>CR:<xsl:text>
</xsl:text>
- 常用编程语言:
LF:"
"CR:"
"
- XSLT:
LF:<xsl:text>
</xsl:text>CR:<xsl:text>
</xsl:text>
Or, if you want to see it in the XML immediately, simply put it in literally:
或者,如果您想立即在 XML 中看到它,只需按字面意思输入:
<?xml version="1.0" encoding="UTF-8" ?>
<item>
<text>Address</text>
<data>
Sample
Text 123
</data>
</item>
Newline still not showing up?
换行符还是不显示?
Keep in mind that how an application interprets text, including newlines, is up to it. If you find that your newlines are being ignored, it might be that the application automatically runs together text separated by newlines.
请记住,应用程序如何解释文本(包括换行符)取决于它。如果您发现您的换行符被忽略,则可能是应用程序自动运行了由换行符分隔的文本。
HTML browsers, for example, will ignore newlines (and will normalize space within text such that multiple spaces are consolidated). To break lines in HTML,
例如,HTML 浏览器将忽略换行符(并将规范文本中的空格,以便合并多个空格)。要在 HTML 中换行,
- use
<br/>; or - wrap block in an element such as a
divorpwhich by default causes a line break after the enclosed text, or in an element such asprewhich by default typically will preserve whitespace and line breaks; or - use CSS styling such as
white-spaceto control newline rendering.
- 使用
<br/>; 或者 - 在元件包裹块诸如
div或p它默认使包含的文本之后的线断裂,或在一个元件,如pre在缺省情况下将通常保留空白和换行符; 或者 - 使用 CSS 样式
white-space来控制换行渲染。
XML application not cooperating?
XML 应用程序不合作?
If an XML application isn't respecting your newlines, and working within the application's processing model isn't helping, another possible recourse is to use CDATAto tell the XML parser notto parse the text containing
the newline.
如果 XML 应用程序不尊重您的换行符,并且在应用程序的处理模型中工作也无济于事,则另一种可能的方法是CDATA告诉 XML 解析器不要解析包含换行符的文本。
<?xml version="1.0" encoding="UTF-8" ?>
<item>
<text>Address</text>
<data>
<![CDATA[Sample
Text 123]]>
</data>
</item>
or, if HTML markup is recognized downstream:
或者,如果下游识别出 HTML 标记:
<?xml version="1.0" encoding="UTF-8" ?>
<item>
<text>Address</text>
<data>
<![CDATA[Sample <br/>
Text 123]]>
</data>
</item>
Whether this helps will depend upon application-defined semantics of one or more stages in the pipeline of XML processing that the XML passes through.
这是否有帮助将取决于 XML 所经过的 XML 处理管道中一个或多个阶段的应用程序定义的语义。
Bottom line
底线
A newline(aka line breakor end-of-line, EOL) can be added much like any character in XML, but be mindful of
甲换行符(又名断线或结束线,EOL)可以加入很像XML任何字符,但应注意到的
- differing OS conventions
- differing XML application semantics
- 不同的操作系统约定
- 不同的 XML 应用程序语义

