在 XML 中添加新的换行符/换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10917555/
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
Adding a new line/break tag in XML
提问by Courtney Jordan
I have been trying to add a new line/break to code within XML and have been unsuccessful.
我一直在尝试向 XML 中的代码添加新的换行符/换行符,但没有成功。
I have tried so far:
到目前为止我已经尝试过:
<br />
<br>


Here is a sample of the code I am working with. I included "
" to show where the break is located within the code.
这是我正在使用的代码示例。我包含了“ 
”来显示中断在代码中的位置。
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>Tootsie roll tiramisu macaroon wafer carrot cake.

 Danish topping sugar plum tart bonbon caramels cake.
</summary>
</item>
采纳答案by Courtney Jordan
The solution to this question is:
这个问题的答案是:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake. <br />
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>
by adding the <br />inside the the <![CDATA]]>this allows the line to break, thus creating a new line!
通过在<br />里面添加 <![CDATA]]>这允许行中断,从而创建一个新行!
回答by arthur
New Line XML
换行 XML
with XML
使用 XML
- Carriage return:

 - Line feed:


- 回车:

 - 换行:


or try like @dj_segfault proposed (see his answer) with CDATA;
或尝试像@dj_segfault 提出的(见他的回答)与 CDATA 一样;
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.]]>
回答by Michael Kay
You don't need anything fancy: the following contains a new line (two, actually):
你不需要任何花哨的东西:以下包含一个新行(实际上是两个):
<summary>Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.
</summary>
The question is, why isn't this newline having the desired effect: and that's a question about what the recipient of the XML is actually doing with it. For example, if the recipient is translating it to HTML and the HTML is being displayed in the browser, then the newline will be converted to a space by the browser. You need to tell us something about the processing pipeline.
问题是,为什么这个换行符没有达到预期的效果:这是一个关于 XML 的接收者实际上用它做什么的问题。例如,如果收件人正在将其翻译为 HTML,并且 HTML 正在浏览器中显示,那么换行符将被浏览器转换为空格。您需要告诉我们一些有关处理管道的信息。
回答by ST3
You are probably using Windows, so new line is CR + LF(carriage return + line feed). So solution would be:
您可能使用的是 Windows,所以换行是CR + LF(回车 + 换行)。所以解决方案是:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>Tootsie roll tiramisu macaroon wafer carrot cake. Danish topping sugar plum tart bonbon caramels cake.
</summary>
</item>
For Linux there is only LFand for Mac OS only CR.
对于 Linux,仅LF适用于 Mac OS CR。
In question there showed Linux way.
有问题有显示Linux方式。
回答by merce_00
This solution worked to me:
这个解决方案对我有用:
<summary>Tootsie roll tiramisu macaroon wafer carrot cake. 
Danish topping sugar plum tart bonbon caramels cake.</summary>
You will have the text in two lines.
您将有两行文本。
This worked to me using the XmlReader.Read method.
这使用 XmlReader.Read 方法对我有用。
回答by dj_segfault
You probably need to put it in a CDATA blockto preserve whitespace
您可能需要将它放在CDATA 块中以保留空格
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>
回答by Gabe Rainbow
Without using CDATA, try
不使用 CDATA,尝试
<xsl:value-of select="'
'" />
Note the double and single quotes.
注意双引号和单引号。
That is particularly useful if you are not creating xml
aka text. <xsl:output method="text" />
如果您不创建 xml aka 文本,这将特别有用。 <xsl:output method="text" />
回答by Yash Kulkarni
The easiest way to give a line break is to do the following :
1> Add the following in CSS - e{display:block}
2> Now wherever you want to give a line break, type -
给换行符的最简单方法是执行以下操作:
1> 在 CSS 中添加以下内容 - e{display:block}
2> 现在,无论您想在何处换行,请键入 -
<e></e>
回答by Rohan Mundhey
The Way to do Line Break in XML is to use 

在 XML 中进行换行的方法是使用 

It worked for me in Eclipse IDE , when I was designing my XML layout & was using TextView.
当我设计 XML 布局并使用 TextView 时,它在 Eclipse IDE 中对我有用。
回答by aprodan
Had same issue when I had to develop a fixed length field format.
当我必须开发固定长度的字段格式时遇到了同样的问题。
Usually we do not use line separator for binary files but For some reason our customer wished to add a line break as separator between records. They set
通常我们不对二进制文件使用换行符,但出于某种原因,我们的客户希望在记录之间添加换行符作为分隔符。他们设
< record_delimiter value="\n"/ >
< record_delimiter value="\n"/ >
but this didn't work as records got two additional characters:
< record1 > \n < record2 > \n.... and so on.
但这不起作用,因为记录有两个额外的字符:
< record1 > \n < record2 > \n.... 等等。
Did following change and it just worked.
做了以下更改,它刚刚奏效。
< record_delimiter value="\n"/> => < record_delimiter value="
"/ >
< record_delimiter value="\n"/> => < record_delimiter value=" "/ >
After unmarshaling Java interprets as new line character.
解组后 Java 解释为换行符。

