开发者

To Read XML file in UTF-8 format string using Android

开发者 https://www.devze.com 2023-01-08 13:31 出处:网络
Respected All, I have to read XML file, for that I use SAXParser and DefaultHandler using method characters(char[] ch, int start, int length) but it gives output with some extra characters such as []

Respected All, I have to read XML file, for that I use SAXParser and DefaultHandler using method characters(char[] ch, int start, int length) but it gives output with some extra characters such as [] in place of '#13'. someone told me that if I read that string in UTF-8 format then it will remove that al开发者_StackOverflow中文版l the extra characters. Is it true that I have to read it in UTF-8 format if yes then how I can read it. Thank You (Vikram Kadam)


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();

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号