【发布时间】:2015-03-05 01:54:14
【问题描述】:
当我从网上下载 xml 时, 会附加到我的 xml 中。
如何从字符串中替换它。我知道我可以使用
子字符串。但是是否有 正则表达式 可以做到这一点..
运行代码是
XmlParser parser = new XmlParser();
xml = parser.getXmlFromUrl(params[0]);
xml = xml.substring(3);
XmLParse 方法
public String getXmlFromUrl(String url)
{
String xml = null;
try
{
//default http client
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
System.out.println("URL IN PARSER:==="+url+"====");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpentity = httpResponse.getEntity();
xml = EntityUtils.toString(httpentity);
Log.d("response", xml);
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return xml;
}
【问题讨论】:
-
你可以使用** replaceAll(String regex, String replacement)** 这个函数
-
听起来更像是你下载不正确
-
如何下载文件?也许已经在那里修复它是值得的。
-
您确定要使用正则表达式删除 BOM,而不是使用正确设置的输入编码?
-
Apache
commons-io提供了可以检测和/或删除 BOM 的BOMInputStream。