title: How to fix the error "org.xml.sax.SAXParseException / Content is not allowed in prolog"?
body: <p>I have following class with two methods. <code>openInputStream</code> creates an input stream, which is subsequently fed into <code>readDocument</code> to create XML document from that stream.</p>
  
  <pre><code>public class XmlReader {
      protected InputStream openInputStream(final String path)
              throws IOException {
          final InputStream inputStream;
          inputStream = IOUtils.toInputStream(path, "UTF-8");
          return inputStream;
      }
      protected Document readDocument(final InputStream stream)
              throws ParserConfigurationException, SAXException, IOException {
          final DocumentBuilderFactory dbfac =
                  DocumentBuilderFactory.newInstance();
          final DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
          return docBuilder.parse(stream);
      }
  }
  </code></pre>
  
  <p>Then I have the following test:</p>
  
  <pre><code>public class XmlReaderTests {
      @Test
      public void parsingOfMapFiles() throws IOException,
              SAXException, ParserConfigurationException {
          final String[] dirs = new String[] {
                  "01_Phoenix1",
                  "02_Phoenix2",
                  "03_Guadalupe",
                  "04_SanDiego_MiramarRoad",
                  "05_Berlin_Alexanderplatz",
                  "06_Portland_ForestAvenue",
                  "07_Hartford_LafayetteHudsonStreet",
                  "08_FortWorth",
                  "09_Charleston_CalhounStreetMeetingStreet",
                  "10_LosAngeles_PershingSquare",
                  "11_Uelzen"
          };
          for (final String dir : dirs) {
              final XmlReader objectUnderTest = new XmlReader();
              final String path =
                      String.format("src/test/resources/mc/E-2015-10-13_1/%s/map.osm",
                              dir);
              final File file = new File(path);
              Assert.assertTrue(file.canRead());
              final InputStream inputStream =
                      objectUnderTest.openInputStream(file.getAbsolutePath());
              final Document doc = objectUnderTest.readDocument(inputStream);
              Assert.assertNotNull(doc);
          }
      }
  }
  </code></pre>
  
  <p><code>readDocument</code> throws the exception</p>
  
  <p><code>
  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:257)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
      at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
      at XmlReader.readDocument(XmlReader.java:26)
      at XmlReaderTests.parsingOfMapFiles(XmlReaderTests.java:40)
  </code></p>
  
  <p>You can find the source code and the XML files I'm trying to read <a href="https://www.evernote.com/shard/s35/sh/d6567a24-d10b-40ae-8ce2-6c1c503fb19c/417e820f284c666618f5c2caf6e7eb62" rel="nofollow">here</a>.</p>
  
  <p>How can I fix this error?</p>
  
  <p><strong>Update 1:</strong> Changing <code>openStream</code> to</p>
  
  <pre><code>protected InputStream openInputStream(final String path)
          throws IOException {
      return new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>protected InputStream openInputStream(final String path)
          throws IOException {
      return new BOMInputStream(IOUtils.toInputStream(path));
  }
  </code></pre>
  
  <p>didn't help.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>If I change the <code>openInputStream</code> method like that</p>
  
  <pre><code>protected InputStream openInputStream(final String path)
          throws IOException {
      BOMInputStream inputStream = new BOMInputStream(IOUtils.toInputStream(path, "UTF-8"));
      System.out.println("BOM: " + inputStream.hasBOM());
      return inputStream;
  }
  </code></pre>
  
  <p>I get this output:</p>
  
  <p><code>
  BOM: false
  [Fatal Error] :1:1: Content is not allowed in prolog.
  </code></p>