2011-06-19 8 views
0

このページから歌詞を取得すると:here、改行が消えます。私は単一のStringで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 
. 
. 

私は、明確にするために数字の背後にある個々の文字を追加した、と明らかに「赤ちゃん」と「犬の間に改行があるはずです。

この改行を解析するにはどうすればよいですか? BufferedReader

+3

*どのように*要素を文字列として取得していますか?短いが完全な*例を提供してください。 –

+2

そのXMLは間違っています。 XMLは空白に敏感ではありません。彼らは改行を脱出する必要があります。会社に苦情を言いなさい。 – SLaks

+0

編集され、解決策が見つかりました。 – nhaarman

答えて

1

を使用し、私はどこからこのソースを使用しました:明らかに

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(); 
} 

、それは「私はこの行を追加したと言う部分"問題だった、このコードを見せてくれたJonに感謝します!

0

ラップStringReaderreadLine()

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

    for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) { 
     // do something with line 
    } 
関連する問題