问题
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