【问题标题】:How to replace  in string java如何在字符串java中替换
【发布时间】: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

标签: java android regex xml


【解决方案1】:

 是 utf-8 中的 BOM。试试这个:

xml = EntityUtils.toString(entity, "utf-8");

【讨论】:

  • 所以它毕竟不是 xml,或者你正在下载一个经过身份验证的用户专用资源......这么多可能性。 html是怎么说的?
  • 是的,我发现我必须使用 php 显示 xml 并使用函数删除它们。我已经直接在服务器上上传了一个xml并尝试直接下载它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2023-02-23
  • 2010-11-17
  • 1970-01-01
  • 2018-03-02
  • 2014-07-23
相关资源
最近更新 更多