XML 中的换行符?
我是 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>
我希望在歌词的句子之间有换行符。我尝试了 /n 中的所有内容以及类似的其他代码、PHP 解析等,但没有任何效果!在网上搜索了几个小时,似乎找不到答案。我使用 XML 将数据插入到使用 Javascript 的 HTML 页面中。
有谁知道如何解决这个问题?
这是我用来将 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>
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:
<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.
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:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
@icktoofay 与 CData 关系密切
@icktoofay was close with the CData
在 XML 中,换行符是一个普通字符。你可以这样做:
并且
的内容将为如果这对你不起作用,那么你做错了什么。不需要像对换行符进行编码这样的特殊“解决方法”。另一方面,像
\n
这样的东西将不起作用,因为 XML 没有转义序列*。* 请注意,
是表示序列化 XML 中换行符的字符实体。 “XML 没有转义序列”是指与 DOM 文档交互、通过 DOM API 设置节点值时的情况。
这是
和
\n
之类的东西都不起作用的地方,但实际换行符可以。该字符如何在序列化文档(即“文件”)中结束由 API 决定,您不必担心。既然您似乎想知道 HTML 中的换行符在哪里:请查看源代码,它们就在那里。 HTML 忽略源代码中的换行符。使用
标签强制在屏幕上换行。下面是一个 JavaScript 函数,它将
插入多行字符串:或者,您可以使用 CSS 在换行符处强制换行:
In XML a line break is a normal character. You can do this:
and the contents of
<text>
will beIf 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*.* Note that
is the character entity that represents a line break in serialized XML. "XML has no escape sequences" means the situation when you interact with a DOM document, setting node values through the DOM API.
This is where neither
nor things like
\n
will work, but an actual newline character will. How this character ends up in the serialized document (i.e. "file") is up to the API and should not concern you.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.Here is a JavaScript function that inserts
<br>
into a multi-line string:Alternatively you can force line breaks at new line characters with CSS:
只需在行尾使用
<br>
即可。just use
<br>
at the end of your lines.在行尾,只需添加以下特殊字符:
该特殊字符定义回车符。
At the end of your lines, simply add the following special character:
That special character defines the carriage-return character.
在 XML 中:使用文字换行符,不需要其他任何东西。
换行符将被保留以供 Javascript 读取它们 [1]。请注意,任何缩进空格以及前后换行符也会被保留(您没有看到它们的原因是 HTML/CSS 默认情况下将空格折叠为单个空格字符)。
那么最简单的方法是: 在 HTML 中:什么都不做,只需使用 CSS 来保留换行符
但这也会保留 XML 文档中的额外行,并且在 IE 6 或 7 中不起作用 [2]。
所以你自己清理空白;这是一种方法(为了清晰起见,换行符 - 无论有没有换行符,Javascript 都满意 [3])[4]
,然后用 or 保留这些换行符
,而不是 CSS,添加
.replace(/ \r?\n/g, '
在其他 JavaScript')
.replace
之后。(旁注:像这样使用 document.write() 也并不理想,有时容易受到跨站点脚本攻击,但这是另一个主题。关于这个答案,如果您想使用替换为
的变体
,你必须转义<
,&
(,>
,"
,'
) 在生成
之前。)--
[1] 参考:“元素空白”部分处理”和“XML 架构空白控制” http://www.usingxml.com/Basics/XmlSpace #ElementWhiteSpaceHandling
[2] http://www.quirksmode.org/css/whitespace。 html
[3] 除了 Javascript 语法中的一些地方其分号插入特别烦人之外
[4] 我编写了它并在 Linux Node.js 中测试了这些正则表达式(它使用与 Chrome 相同的 Javascript 引擎,“ V8”)。某些浏览器以不同方式执行正则表达式的风险很小。 (我的测试字符串(在 JavaScript 语法中)
"\n\nfoo bar baz\n\n\tmore歌词\nare good\n\n"
)In the XML: use literal line-breaks, nothing else needed there.
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).
Then the easiest way is: In the HTML: do nothing, just use CSS to preserve the line-breaks
But this also preserves your extra lines from the XML document, and doesn't work in IE 6 or 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]
and then preserve those line-breaks with
or, instead of that CSS, add a
.replace(/\r?\n/g, '<br />')
after the other 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<
,&
(,>
,"
,'
) before generating the<br>
s.)--
[1] reference: sections "Element White Space Handling" and "XML Schema White Space Control" http://www.usingxml.com/Basics/XmlSpace#ElementWhiteSpaceHandling
[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.
[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"
)(以Home on the Range的曲调演唱)
如果是我的,我也会将副歌部分和主歌部分封装在 XML 元素中。
(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.
如果您使用 CDATA,我认为您可以将换行符直接嵌入到 XML 中。例子:
If you use CDATA, you could embed the line breaks directly into the XML I think. Example:
如果您使用 CSS 进行样式设置(不推荐。),您可以使用
display:block;
,但是,这只会给您换行符 before 和 after< /em> 样式元素。If you are using CSS to style (Not recommended.) you can use
display:block;
, however, this will only give you line breaks before and after the styled element.