且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

要采用Android阅读UTF-8格式的字符串的XML文件

更新时间:2023-11-06 18:47:04

我用这个来解析与SAXParser的:

I use this to parse with the SAXparser :

URL url = new URL(urlToParse);
SAXParserFactory spf = SAXParserFactory.newInstance();
// here we get our SAX parser
SAXParser sp = spf.newSAXParser();
// we fuse it to a XML reader
XMLReader xr = sp.getXMLReader();
DefaultHandler handlerContact = new DefaultHandler();
// we give it a handler to manage the various events
xr.setContentHandler(handlerContact);
// and finally we open the stream to the url
InputStream oS = url.openStream();
// and parse it
xr.parse(new InputSource(new InputStreamReader(oS, Charset.forName("utf-8"))));
// to retrieve the list of contacts created by the handler
result = handlerContact.getEntries();
// don't forget to close the resource
oS.close();

我从未有过任何麻烦,只要你解析初始文件是正确的连接codeD的UTF-8。检查它是否是,因为有时,当您使用电脑的默认配置,默认情况下是不是UTF-8,但ANSI或ISO-8859-1

I never had any trouble as long as the initial file you are parsing is properly encoded in UTF-8. Check if it is, because sometimes, when you use default configuration of your computer, default is not UTF-8 but ANSI or ISO-8859-1