Java Saxparser:格式不正确(无效令牌)

Java Saxparser:格式不正确(无效令牌),java,android,encoding,saxparser,Java,Android,Encoding,Saxparser,我复制并编辑了一些代码,然后在我的手机上从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>ä</roo

我复制并编辑了一些代码,然后在我的手机上从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();
      }
试试看{
File myFile=新文件(Storage.CONTENT_CACHE,“test.xml”);
如果(!myFile.exists()){
字符串xml=“ä”;
OutputStreamWriter wrt=新的OutputStreamWriter(新文件OutputStream(myFile),“Cp1252”);
wrt.write(xml);
wrt.close();
}
SAXParserFactory sf=SAXParserFactory.newInstance();
SAXParser p=sf.newSAXParser();
InputSource InputSource=新的InputSource();
setByteStream(新文件InputStream(myFile));
p、 解析(inputSource,新的DefaultHandler(){
公共无效字符(char[]ch,int start,int length)引发异常{
字符串测试=字符串.valueOf(ch,start,length);
字符串s=“breakpt”;
}
});
}捕获(例外e){
e、 printStackTrace();
}

有人能解释一下为什么我收到“格式不正确(无效令牌)”的异常消息吗?

也许xml不接受任何编码,搜索“xml有效编码”

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

异常应该会告诉您哪个令牌无效。检查
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();
      }