title: Error in parsing XML file with encoding UTF-8 and UTF-16
body: <p>I am using below method to parse an XML file -</p>
  
  <pre><code>package com.kcs.xml;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.xml.sax.InputSource;
  
  public class ParseXMLOld {
      public static void main(String[] args) 
      {
          final String FILE_PATH="C:\\abc.xml";
          File file=new File(FILE_PATH);
          ParseXMLOld pxo=new ParseXMLOld();
          pxo.parseUTFXML(file);
      }
  
      public Document parseUTFXML(File file) 
      {
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder=null;
          Document doc=null;
          try {
              docBuilder = docBuilderFactory.newDocumentBuilder();
              InputStream inputStream= new FileInputStream(file);
              Reader reader = new InputStreamReader(inputStream,"UTF-16");
              InputSource is = new InputSource(reader);
              is.setEncoding("UTF-16");
              doc = docBuilder.parse(is);
              System.out.println("Done");
              } 
          catch(Exception e)
              {
              e.printStackTrace();
              }
          finally
              {
              docBuilderFactory=null;
              docBuilder=null;
              }
      return doc;
      }
  }
  </code></pre>
  
  <p>I have two file with encoding UTF-8 and UTF-16. If value of "UTF_ENCODING" in above code is UTF-8 then the file with "UTF-8" encoding works fine. But my code fails to parse other file with encoding "UTF-16" and vice versa.</p>
  
  <p>I would like to mention one more interesting thing, if I create an sample XML file manually with encoding UTF-16, IE7 fails to open it. But the file with UTF-16 encoding, that I am trying to parse (I am getting it from another system) is opening in IE7. But if you edit the first line of this file (change encoding to UTF-8 and then change it to UTF-16 again), it doesn't open. I have no idea why this is happening.</p>
  
  <p>Please help.</p>
  
  <p>I dont know how I can share these file. If required please tell me how can I share these two files?</p>
  
  <p><strong>For example, how can I parse below file?</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
    &lt;Details&gt;
      &lt;Content&gt;
        &lt;id&gt;1234¥£€$¢&lt;/id&gt;
        &lt;Valid_From_Date&gt;2013-01-01&lt;/Valid_From_Date&gt;
        &lt;Valid_To_Date&gt;9999-12-31&lt;/Valid_To_Date&gt;
        &lt;Company&gt;1210&lt;/Company&gt;
        &lt;Description&gt;2nd Life Transaction&lt;/Description&gt;
      &lt;/Content&gt;
      &lt;Totals&gt;
        &lt;Count&gt;1&lt;/Count&gt;
      &lt;/Totals&gt;
  &lt;/Details&gt;
  </code></pre>
  
  <p>I am getting below error -</p>
  
  <p>[Fatal Error] :1:1: Content is not allowed in prolog.
  org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:251)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)
      at com.kcs.xml.ParseXMLOld.parseUTF8XML(ParseXMLOld.java:34)
      at com.kcs.xml.ParseXMLOld.main(ParseXMLOld.java:19)</p>