如何从 xml 中保存换行符?
当我从此页面检索歌词时: 这里,换行符消失了。我在单个字符串中检索 xml,为了简单起见,我使用子字符串来减去歌词。当我使用以下代码打印每个字符时,看不到换行符。
if (response.contains("lyrics_body")) {
lyrics = response.substring(response.indexOf("<lyrics_body>") + 13, response.indexOf("</lyrics_body>"));
StringReader sr = new StringReader(lyrics);
int l;
try {
while ((l = sr.read()) != -1) {
System.out.println(":" + l);
}
} catch (Exception ex) {
}
}
部分输出:
85 U
104 h
110 n
32
116 t
105 i
115 s
115 s
32
117 u
104 h
110 n
32
116 t
105 i
115 s
115 s
32
117 u
104 h
110 n
32
116 t
105 i
115 s
115 s
32
98 b
97 a
98 b
121 y
68 d
111 o
103 g
32
119 w
105 i
108 l
108 l
.
.
为了清楚起见,我在数字后面添加了各个字符,显然在“baby”和“dog”之间应该有一个换行符。
我怎样才能解析这个换行符?
When I retrieve lyrics from this page: here, the newlines disappear. I retrieve the xml in single String, and for simpleness for this question I use substring to subtract the lyrics. When I print each character using the following code, no newlines are visible.
if (response.contains("lyrics_body")) {
lyrics = response.substring(response.indexOf("<lyrics_body>") + 13, response.indexOf("</lyrics_body>"));
StringReader sr = new StringReader(lyrics);
int l;
try {
while ((l = sr.read()) != -1) {
System.out.println(":" + l);
}
} catch (Exception ex) {
}
}
Part of the output:
85 U
104 h
110 n
32
116 t
105 i
115 s
115 s
32
117 u
104 h
110 n
32
116 t
105 i
115 s
115 s
32
117 u
104 h
110 n
32
116 t
105 i
115 s
115 s
32
98 b
97 a
98 b
121 y
68 d
111 o
103 g
32
119 w
105 i
108 l
108 l
.
.
I've added the individual chars behind the numbers for clarity, and obviously between 'baby' and 'dog' there should be a newline.
How can I parse this newline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从某个地方使用了这个来源:
显然,它说“我刚刚添加了这一行”的部分是问题所在,感谢乔恩让我查看这段代码!
I've used this source from somewhere:
Obviously, the part where it says "I've just added this line" was the problem, thanks Jon for making me look in this code!
将
StringReader
包装在BufferedReader
中并使用readLine()
wrap
StringReader
in aBufferedReader
and usereadLine()