Saxparser: not well-formed (invalid token)

為{幸葍}努か 提交于 2019-12-13 04:17:36

问题


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)"?


回答1:


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




回答2:


Maybe xml doesn't accept any encoding, search for 'xml valid encodings'



来源:https://stackoverflow.com/questions/54715449/saxparser-not-well-formed-invalid-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!