如何从 xml 中保存换行符?

发布于 2024-11-16 09:41:05 字数 1054 浏览 1 评论 0原文

当我从此页面检索歌词时: 这里,换行符消失了。我在单个字符串中检索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-11-23 09:41:05

我从某个地方使用了这个来源:

public static String postData(String base, List<BasicNameValuePair> data) throws IOException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(base);
    HttpResponse response = null;
    String line = "";
    StringBuilder total = new StringBuilder();
    HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(data));

        // Execute HTTP Post Request
        response = httpclient.execute(httppost);

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        // Read response until the end
        while ((line = rd.readLine()) != null) {
            total.append(line);
            total.append("\n"); //I'VE JUST ADDED THIS LINE
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Return full string
    return total.toString();
}

显然,它说“我刚刚添加了这一行”的部分是问题所在,感谢乔恩让我查看这段代码!

I've used this source from somewhere:

public static String postData(String base, List<BasicNameValuePair> data) throws IOException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(base);
    HttpResponse response = null;
    String line = "";
    StringBuilder total = new StringBuilder();
    HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);

    try {
        httppost.setEntity(new UrlEncodedFormEntity(data));

        // Execute HTTP Post Request
        response = httpclient.execute(httppost);

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        // Read response until the end
        while ((line = rd.readLine()) != null) {
            total.append(line);
            total.append("\n"); //I'VE JUST ADDED THIS LINE
        }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Return full string
    return total.toString();
}

Obviously, the part where it says "I've just added this line" was the problem, thanks Jon for making me look in this code!

梦归所梦 2024-11-23 09:41:05

StringReader 包装在 BufferedReader 中并使用 readLine()

    BufferedReader bufferedReader = new BufferedReader(new StringReader(lyrics));

    for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
        // do something with line
    }

wrap StringReader in a BufferedReader and use readLine()

    BufferedReader bufferedReader = new BufferedReader(new StringReader(lyrics));

    for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
        // do something with line
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文