Html XML 中的换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2986297/
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
Line Break in XML?
提问by ew89
I'm a beginner in web development, and I'm trying to insert line breaks in my XML file. This is what my XML looks like:
我是 Web 开发的初学者,我正在尝试在我的 XML 文件中插入换行符。这是我的 XML 的样子:
<musicpage>
<song>
<title>Song Title</title>
<lyric>Lyrics</lyric>
</song>
<song>
<title>Song Title</title>
<lyric>Lyrics</lyric>
</song>
<song>
<title>Song Title</title>
<lyric>Lyrics</lyric>
</song>
<song>
<title>Song Title</title>
<lyric>Lyrics</lyric>
</song>
</musicpage>
I want to have line breaks in between the sentences for the lyrics. I tried everything from /n, and other codes similar to it, PHP parsing, etc., and nothing works! Have been googling online for hours and can't seem to find the answer. I'm using the XML to insert data to an HTML page using Javascript.
我想在歌词的句子之间换行。我尝试了 /n 中的所有内容,以及其他类似的代码、PHP 解析等,但没有任何效果!已经在网上搜索了几个小时,似乎找不到答案。我正在使用 XML 将数据插入使用 Javascript 的 HTML 页面。
Does anyone know how to solve this problem?
有谁知道如何解决这个问题?
And this is the JS code I used to insert the XML data to the HTML page:
这是我用来将 XML 数据插入 HTML 页面的 JS 代码:
<script type="text/javascript">
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
} else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","xml/musicpage_lyrics.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
var x=xmlDoc.getElementsByTagName("songs");
for (i=0;i<x.length;i++) {
document.write("<p class='msg_head'>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</p><p class='msg_body'>");
document.write(x[i].getElementsByTagName("lyric")[0].childNodes[0].nodeValue);
document.write("</p>");
}
</script>
回答by Chase Florell
@icktoofay was close with the CData
@icktoofay 与 CData 关系密切
<myxml>
<record>
<![CDATA[
Line 1 <br />
Line 2 <br />
Line 3 <br />
]]>
</record>
</myxml>
回答by Tomalak
In XML a line break is a normal character. You can do this:
在 XML 中,换行符是一个普通字符。你可以这样做:
<xml>
<text>some text
with
three lines</text>
</xml>
and the contents of <text>
will be
和内容<text>
将是
some text with three lines
If this does not work for you, you are doing something wrong. Special "workarounds" like encoding the line break are unnecessary. Stuff like \n
won't work, on the other hand, because XML has no escape sequences*.
如果这对您不起作用,那么您做错了。不需要像对换行符进行编码这样的特殊“解决方法”。\n
另一方面,像这样的东西是行不通的,因为 XML 没有转义序列*。
*Note that 

is the character entity that represents a line break in serializedXML. "XML has no escape sequences" means the situation when you interact with a DOM document, setting node values through the DOM API.
*请注意,这

是表示序列化XML 中换行符的字符实体。“XML 没有转义序列”是指当您与 DOM 文档交互,通过 DOM API 设置节点值时的情况。
This is where neither 

nor things like \n
will work, but an actualnewline character will. How this character ends up in the serialized document (i.e. "file") is up to the API and should not concern you.
这是既不可行

也不行的地方\n
,但实际的换行符可以。这个字符如何在序列化文档(即“文件”)中结束取决于 API,您不应该关心。
Since you seem to wonder where your line breaks go in HTML: Take a look into your source code, there they are. HTML ignores line breaks in source code. Use <br>
tags to force line breaks on screen.
由于您似乎想知道 HTML 中的换行符在哪里:查看您的源代码,它们就在那里。HTML 忽略源代码中的换行符。使用<br>
标签在屏幕上强制换行。
Here is a JavaScript function that inserts <br>
into a multi-line string:
这是一个插入<br>
多行字符串的 JavaScript 函数:
function nl2br(s) { return s.split(/\r?\n/).join("<br>"); }
Alternatively you can force line breaks at new line characters with CSS:
或者,您可以使用 CSS 在换行符处强制换行:
div.lines {
white-space: pre-line;
}
回答by KushalP
At the end of your lines, simply add the following special character: 
在行的末尾,只需添加以下特殊字符: 
That special character defines the carriage-return character.
该特殊字符定义了回车符。
回答by Abolfazl Beigi
just use <br>
at the end of your lines.
只需<br>
在行尾使用即可。
回答by idupree
In the XML: use literal line-breaks, nothing else needed there.
在 XML 中:使用文字换行符,那里不需要其他任何东西。
The newlines will be preserved for Javascript to read them [1]. Note that any indentation-spaces and preceding or trailing line-breaks are preserved too (the reason you weren't seeing them is that HTML/CSS collapses whitespace into single space-characters by default).
换行符将保留供 Javascript 读取 [1]。请注意,任何缩进空格和前后换行符也会被保留(您没有看到它们的原因是 HTML/CSS 默认将空格折叠为单个空格字符)。
Then the easiest way is: In the HTML: do nothing, just use CSS to preserve the line-breaks
那么最简单的方法是:在 HTML 中:什么都不做,只需使用 CSS 来保留换行符
.msg_body {
white-space: pre-line;
}
But this also preserves your extra lines from the XML document, and doesn't work in IE 6 or 7 [2].
但这也保留了 XML 文档中的额外行,并且不适用于 IE 6 或 7 [2]。
So clean up the whitespace yourself; this is one way to do it (linebreaks for clarity - Javascript is happy with or without them [3]) [4]
所以自己清理空白;这是一种方法(为了清晰起见,使用换行符 - Javascript 对有或没有它们感到满意 [3])[4]
[get lyric...].nodeValue
.replace(/^[\r\n\t ]+|[\r\n\t ]+$/g, '')
.replace(/[ \t]+/g, ' ')
.replace(/ ?([\r\n]) ?/g, '')
and then preserve those line-breaks with
然后保留这些换行符
.msg_body {
white-space: pre; // for IE 6 and 7
white-space: pre-wrap; // or pre-line
}
or, instead of that CSS, add a .replace(/\r?\n/g, '<br />')
after the other JavaScript .replace
s.
或者,代替那个 CSS,.replace(/\r?\n/g, '<br />')
在其他 JavaScript .replace
s之后添加一个。
(Side note: Using document.write() like that is also not ideal and sometimes vulnerable to cross-site scripting attacks, but that's another subject. In relation to this answer, if you wanted to use the variation that replaces with <br>
, you'd have to escape <
,&
(,>
,"
,'
) beforegenerating the <br>
s.)
(旁注:像这样使用 document.write() 也不理想,有时容易受到跨站点脚本攻击,但这是另一个主题。关于此答案,如果您想使用替换为 的变体,则<br>
“ d 必须在生成s之前转义<
, &
(, >
, "
, '
) 。)<br>
--
——
[1] reference: sections "Element White Space Handling" and "XML Schema White Space Control" http://www.usingxml.com/Basics/XmlSpace#ElementWhiteSpaceHandling
[1] 参考:“元素空白处理”和“XML 模式空白控制”部分http://www.usingxml.com/Basics/XmlSpace#ElementWhiteSpaceHandling
[2] http://www.quirksmode.org/css/whitespace.html
[2] http://www.quirksmode.org/css/whitespace.html
[3] except for a few places in Javascript's syntax where its semicolon insertion is particularly annoying.
[3] 除了 Javascript 语法中的一些地方,它的分号插入特别烦人。
[4] I wrote it and tested these regexps in Linux Node.js (which uses the same Javascript engine as Chrome, "V8"). There's a small risk some browser executes regexps differently. (My test string (in javascript syntax) "\n\nfoo bar baz\n\n\tmore lyrics \nare good\n\n"
)
[4] 我在 Linux Node.js 中编写并测试了这些正则表达式(它使用与 Chrome 相同的 Javascript 引擎,“V8”)。某些浏览器以不同方式执行正则表达式的风险很小。(我的测试字符串(在 javascript 语法中)"\n\nfoo bar baz\n\n\tmore lyrics \nare good\n\n"
)
回答by Robusto
<song>
<title>Song Tigle</title>
<lyrics>
<line>The is the very first line</line>
<line>Number two and I'm still feeling fine</line>
<line>Number three and a pattern begins</line>
<line>Add lines like this and everyone wins!</line>
</lyrics>
</song>
(Sung to the tune of Home on the Range)
(唱《牧场上的家》的曲调)
If it was mine I'd wrap the choruses and verses in XML elements as well.
如果是我的,我也会用 XML 元素包装合唱和诗句。
回答by icktoofay
If you use CDATA, you could embed the line breaks directly into the XML I think. Example:
如果您使用 CDATA,您可以将换行符直接嵌入到我认为的 XML 中。例子:
<song>
<title>Song Title</title>
<lyric><![CDATA[Line 1
Line 2
Line 3]]></lyric>
</song>
回答by sohaib
<description><![CDATA[first line<br/>second line<br/>]]></description>
回答by Ben
If you are using CSS to style (Not recommended.) you can use display:block;
, however, this will only give you line breaks beforeand afterthe styled element.
如果您使用 CSS 来设置样式(不推荐。)您可以使用display:block;
,但是,这只会在样式元素之前和之后为您提供换行符。