title: Howto let the SAX parser determine the encoding from the xml declaration?
body: <p>I'm trying to parse xml files from different sources (over which I have little control). Most of the them are encoded in UTF-8 and don't cause any problems using the following snippet:</p>
  
  <pre><code>SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = factory.newSAXParser();
  FeedHandler handler = new FeedHandler();
  InputSource is = new InputSource(getInputStream());
  parser.parse(is, handler);
  </code></pre>
  
  <p>Since SAX defaults to UTF-8 this is fine. However some of the documents declare:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  </code></pre>
  
  <p>Even though ISO-8859-1 is declared SAX still defaults to UTF-8.
  Only if I add:</p>
  
  <pre><code>is.setEncoding("ISO-8859-1");
  </code></pre>
  
  <p>Will SAX use the correct encoding.</p>
  
  <p>How can I let SAX automatically detect the correct encoding from the xml declaration without me specifically setting it? I need this because I don't know before hand what the encoding of the file will be.</p>
  
  <p>Thanks in advance,
  Allan</p>