本文介绍了XML解析:编码utf-8 &UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析此 LINK,它的xml编码是这样的

<?xml version="1.0" encoding="utf-8"?>

当我尝试 get response 时在 logcat 中抛出消息,如图所示

11-19 17:25:13.350: W/System.err(3360): 这是无效的 URL11-19 17:25:13.350: W/System.err(3360): java.lang.NullPointerException

当我尝试使用其他一些 LINK 时,其编码是这样的p>

<?xml version="1.0" encoding="UTF-8"?> 工作正常,我可以解析值.

xml 解析失败是因为编码不是 UTF-8,它是 utf-8 吗??

我应该如何处理这个问题.我做过谷歌,对 XML 解析很陌生.这是我尝试创建的第一个解析.请告诉我路.

已更新代码:

public String getXmlFromUrl(String url) {字符串 xml = null;尝试 {//默认HttpClientHttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = 新的 HttpPost(url);HttpResponse httpResponse = httpClient.execute(httpPost);HttpEntity httpEntity = httpResponse.getEntity();xml = EntityUtils.toString(httpEntity);System.out.println("响应 --" + xml);} 捕捉(UnsupportedEncodingException e){e.printStackTrace();} 捕捉(ClientProtocolException e){e.printStackTrace();} 捕捉(IOException e){e.printStackTrace();}//返回 XML返回xml;}
解决方案

看起来问题在于响应中的 XML 编码.

URL url = new URL("http://myurl.com");InputSource is = new InputSource(url.openStream());is.setEncoding("ISO-8859-1");//也试试 UTF-8 或 UTF-16BufferedReader br = new BufferedReader(new InputStreamReader(is.getByteStream()));字符串线,str;while((line=br.readLine())!=null){str = str + 线;}日志.i(标签,str);

I am trying to parse the values from this LINK, whose xml encoding is like this

<?xml version="1.0" encoding="utf-8"?>

when I tried to get response throws message in logcat as shown

11-19 17:25:13.350: W/System.err(3360): This is not valid URL
11-19 17:25:13.350: W/System.err(3360): java.lang.NullPointerException

When I tried with some other LINK ,whose encoding is like this

<?xml version="1.0" encoding="UTF-8"?> It works fine, I can parse the values.

is xml parsing failing due encoding not being UTF-8,it is utf-8 ??

How should I deal with this. I have done google and am new to XML parsing. This is my first parsing which I am trying to create. Please let me know the way.

updated with code :

public String getXmlFromUrl(String url) {

    String xml = null;

    try {
        // defaultHttpClient
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
        System.out.println("response -- " + xml);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}
解决方案

Straight away it looks like the problem is with Encoding of XML in your response.

URL url = new URL("http://myurl.com");
InputSource is = new InputSource(url.openStream());
is.setEncoding("ISO-8859-1"); // Also Try UTF-8 or UTF-16
BufferedReader br = new BufferedReader(new InputStreamReader(is.getByteStream()));
String line,str;
while((line=br.readLine())!=null)
{
      str = str + line;
}
Log.i(TAG,str);

这篇关于XML解析:编码utf-8 &amp;UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 07:16
查看更多