Java 如何从xml保存换行符?
当我从这个页面检索歌词时,换行符消失了。我以单个字符串检索xml,为了简化这个问题,我使用子字符串减去歌词。当我使用以下代码打印每个字符时,没有可见的换行符Java 如何从xml保存换行符?,java,xml,newline,Java,Xml,Newline,当我从这个页面检索歌词时,换行符消失了。我以单个字符串检索xml,为了简化这个问题,我使用子字符串减去歌词。当我使用以下代码打印每个字符时,没有可见的换行符 if (response.contains("lyrics_body")) { lyrics = response.substring(response.indexOf("<lyrics_body>") + 13, response.indexOf("</lyrics_body>"));
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) {
}
}
为了清晰起见,我在数字后面添加了单独的字符,显然在“baby”和“dog”之间应该有一个新行
如何解析此换行符?在
BufferedReader
中包装StringReader
,并使用readLine()
我从某处使用过这个来源:
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();
}
公共静态字符串postData(字符串基、列表数据)引发IOException{
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(基础);
HttpResponse响应=null;
字符串行=”;
StringBuilder总计=新StringBuilder();
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(),false);
试一试{
setEntity(新的UrlEncodedFormEntity(数据));
//执行HTTP Post请求
response=httpclient.execute(httppost);
//在InputStream周围包装一个BufferedReader
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
//读取响应直到结束
而((line=rd.readLine())!=null){
合计.追加(行);
total.append(“\n”);//我刚刚添加了这一行
}
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//返回完整字符串
返回total.toString();
}
显然,它说“我刚刚添加了这一行”的部分是问题所在,谢谢Jon让我看这段代码 如何将元素作为字符串检索?请提供一个简短但完整的示例。该XML是错误的。XML不区分空格;他们需要摆脱他们的新台词。向公司投诉。
BufferedReader bufferedReader = new BufferedReader(new StringReader(lyrics));
for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
// do something with line
}
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();
}