久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Sax - ExpatParser$ParseException

Sax - ExpatParser$ParseException(Sax - ExpatParser$ParseException)
本文介紹了Sax - ExpatParser$ParseException的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在制作一個(gè)讀取 XML Internet 的 Android 應(yīng)用程序.此應(yīng)用程序使用 SAX 來(lái)解析 XML.這是我的解析部分代碼:

I'm making an Android application that reads an XML Internet. This application uses SAX to parse XML. This is my code for the part of parsing:

public LectorSAX(String url){
    try{
        SAXParserFactory spf=SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        DefaultHandler lxmlr=new LibraryXMLReader() ;
        sp.parse(url, lxmlr);

        nodo=((LibraryXMLReader)lxmlr).getNodoActual();

    }catch(ParserConfigurationException e){ 
        System.err.println("Error de parseo en LectorSAX.java: "+e);
    }catch(SAXException e){
        System.err.println("Error de sax LectorSAX.java: " + e);
    } catch (IOException e){
        System.err.println("Error de  io LectorSAX.java: " + e);
    }
}

問(wèn)題是發(fā)生了 SAXException.異常信息如下:

The problem is that SAXException occurs. The exception message is as follows:

org.apache.harmony.xml.ExpatParser$ParseException:在第 4 行,第 4 列42:格式不正確(無(wú)效令牌)

org.apache.harmony.xml.ExpatParser$ParseException: At line 4, column 42: not well-formed (invalid token)

但是,如果我將相同的代碼放入普通的 Java SE 應(yīng)用程序中,則不會(huì)發(fā)生此異常并且一切正常.

However, if I put the same code in a normal Java SE application, this exception does not occur and everything works fine.

為什么相同的代碼在 Java SE 應(yīng)用程序中運(yùn)行良好,而不是在 Android 中運(yùn)行?另一方面,如何解決這個(gè)問(wèn)題?

Why the same code works fine in a Java SE application, not an Android?. On the other hand, How to solve the problem?.

感謝您的幫助.

您好.

推薦答案

這可能是字符編碼問(wèn)題.
如您所見(jiàn),無(wú)效令牌錯(cuò)誤指向第 4 行.
在這一行中,您可以找到一個(gè)銳號(hào) (Meteorología) 和一個(gè)波浪號(hào) (Espa?a).XML 標(biāo)頭顯示 ISO-8859-15 編碼值.由于它不如 UTF 或 ISO-8859-1 編碼常見(jiàn),因此當(dāng) SAXParser 連接并嘗試使用系統(tǒng)默認(rèn)字符集將字節(jié)內(nèi)容轉(zhuǎn)換為字符時(shí),這可能會(huì)導(dǎo)致錯(cuò)誤.

This could be a character encoding problem.
As you can see, the invalid token error points to the line #4.
In this line, you can find an acute (Meteorología) and a tilde (Espa?a). The XML header shows a ISO-8859-15 encoding value. As it's less common than UTFs or ISO-8859-1 encodings, this could result in a error when the SAXParser connects and try to convert the byte content into chars using your system default charset.

然后,您需要告訴 SAXParser 使用哪個(gè)字符集.一種方法是傳遞 InputSource,而不是 URL,到 parse 方法.舉個(gè)例子:

Then, you'll need to tell the SAXParser which charset to use. A way to do so, is to pass an InputSource, instead of the URL, to the parse method. As an example:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();

InputSource is = new InputSource(url);
is.setEncoding("ISO-8859-15");

DefaultHandler lxmlr=new LibraryXMLReader() ;
sp.parse(is, lxmlr);

Android VM 似乎不支持這種編碼,拋出 org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: unknown encoding 異常.
作為 ISO-8859-15,它主要與 ISO-8859-1 兼容,除了一些特定字符(如您所見(jiàn) here),解決方法是在 setEncoding 方法中將 ISO-8859-15 值更改為 ISO-8859-1,強(qiáng)制解析器使用不同但兼容的字符集編碼:

It seems that Android VM does not support this encoding, throwing a org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: unknown encoding exception.
As ISO-8859-15 it's mainly compatible with ISO-8859-1, except some specific characters (as you can see here), a workaround is changing the ISO-8859-15 value to ISO-8859-1 at the setEncoding method, forcing the parser to use a different but compatible charset encoding:

is.setEncoding("ISO-8859-1");

看起來(lái),由于 Android 不支持聲明的字符集,它使用其默認(rèn) (UTF-8),因此解析器無(wú)法使用 XML 聲明來(lái)選擇適當(dāng)?shù)木幋a.

As it seems, as Android doesn't support the declared charset, it uses its default (UTF-8) and hence the parser can't use the XML declaration to choose the apropiate encoding.

這篇關(guān)于Sax - ExpatParser$ParseException的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽(tīng)器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無(wú)效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 国产美女在线看 | 羞羞视频网站免费观看 | 日韩免费网站 | 亚洲播放 | 亚洲成人自拍 | 久久国产欧美日韩精品 | 精品久久久久一区 | 国产一级视频免费播放 | 99热最新网址 | 精品欧美一区二区在线观看欧美熟 | 精品久久国产 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 亚洲欧美国产精品久久 | av中文在线 | 国产精品成av人在线视午夜片 | 久久一级大片 | 国产99在线 | 欧美 | 小草久久久久久久久爱六 | 国产女人与拘做受免费视频 | 激情毛片 | 日韩免费看片 | 免费国产一区二区 | 九九久久久 | 中文字幕在线视频一区二区三区 | 免费视频二区 | 日韩精品久久久久 | 午夜精品视频 | 国产线视频精品免费观看视频 | 成人黄色三级毛片 | 日韩av成人在线 | 中文精品视频 | 天天干天天想 | 久久久久国产精品午夜一区 | 无码国模国产在线观看 | 午夜精品一区二区三区在线视频 | 精品国产一区二区在线 | 国产精品久久av | 男人天堂手机在线视频 | 久久精品国产免费 | 伊人无码高清 | 国产精品亚洲精品日韩已方 |