5

I am trying to get xml file from url link . This code for android. I am using SAXParser for doing it. but I have error:

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 87: not well-formed (invalid token)

on this line

 saxParser.parse(url.openStream(),handler);

my code is:

URL url = null;

try {
    url = new URL("toryRss1854.xml");
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try {
    saxParser = factory.newSAXParser();
    saxParser.parse(url.openStream(),handler);

What can be done?

Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89

1 Answers1

3

The problem would appear to be that the XML you are trying to parsing is not UTF-8 so you need to tell SAX which encoding to use.

something like this should work (although not tested this)

InputSource is = new InputSource(url.openStream());
is.setEncoding("Cp1255");
parser.parse(is, handler);
rhinds
  • 9,976
  • 13
  • 68
  • 111
  • The docs say that you don't have to specify an encoding; SaxParser should be able to autodetect the encoding – DennisVA Feb 15 '19 at 18:09