XML解析:编码UTF-8和; UTF-8 [英] XML parsing : encoding utf-8 & UTF-8

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

问题描述

我试图解析从这个链接,它的XML编码是这样的

< XML版本=1.0编码=UTF-8>

当我试图得到响应抛出logcat的消息显示

  11-19 17:25:13.350:W / System.err的(3360):这不是合法的URL
11-19 17:25:13.350:W / System.err的(3360):显示java.lang.NullPointerException
 

当我试着用一些其他的 LINK ,其编码是这样的

< XML版本=1.0编码=UTF-8> 它工作正常,我可以分析值

是XML解析失败是由于编码不是UTF-8,这是UTF-8 ??

我应该如何处理这个问题。我做了谷歌,并相信新的XML解析。这是我的,我想创建第一个解析。请让我知道的方式。

更新code:

 公共字符串getXmlFromUrl(字符串URL){

    XML字符串= NULL;

    尝试 {
        // defaultHttpClient
        HttpClient的HttpClient的=新DefaultHttpClient();
        HttpPost httpPost =新HttpPost(URL);
        HTT presponse HTT presponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = HTT presponse.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(http://myurl.com);
InputSource的是=新的InputSource(url.openStream());
is.setEncoding(ISO-8859-1); //也可以尝试UTF-8或UTF-16
的BufferedReader BR =新的BufferedReader(新的InputStreamReader(is.getByteStream()));
串线,STR;
而((行= br.readLine())!= NULL)
{
      海峡= STR +线;
}
Log.i(TAG,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和; UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆