Saxparser:格式不正确(令牌无效) [英] Saxparser: not well-formed (invalid token)

查看:69
本文介绍了Saxparser:格式不正确(令牌无效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我复制并编辑了一些代码,然后在Android Studio的手机上运行了此代码:

I copied and edited some code, then ran this on my phone from Android Studio:

try {
         File myFile = new File(Storage.CONTENT_CACHE, "test.xml");
         if(!myFile.exists()) {
                String xml = "<?xml version='1.0' encoding='windows-1252'?><root>ä</root>";
                OutputStreamWriter wrt = new OutputStreamWriter(new FileOutputStream(myFile), "Cp1252");
                wrt.write(xml);
                wrt.close();
         }

         SAXParserFactory sf = SAXParserFactory.newInstance();
         SAXParser p = sf.newSAXParser();
         InputSource inputSource = new InputSource();
         inputSource.setByteStream(new FileInputStream(myFile));
         p.parse(inputSource, new DefaultHandler() {
                    public void characters(char[] ch, int start, int length) throws SAXException {
                        String test = String.valueOf(ch, start, length);
                        String s = "breakpt";
                    }
                });

      } catch (Exception e) {
                e.printStackTrace();
      }

有人可以解释为什么我收到异常消息不好格式(无效令牌)?

Can someone please explain why i'm getting the exception with message "not well-formed (invalid token)"?

推荐答案

似乎您需要使用 inputSource.setEncoding( windows -1252)。其他库尝试检测字符集,但我找不到任何可靠的字符集。

Seems you need to use inputSource.setEncoding("windows-1252") . Other libraries tries to detect charset but I couldn't find any robust one.

这篇关于Saxparser:格式不正确(令牌无效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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