Error parsing XML: Content is not allowed in prolog
<p>This is an XML document (the sentence and whitespace prior to the XML declaration and XSLT processing instruction are part of the input):</p>

<pre><code>This XML file does not appear to have any style information associated with it. The document tree is shown below.


    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;?xml-stylesheet type="text/xsl" href="/3.0/style/exchange.xsl"?&gt;
      &lt;mts:meta name="elapsed-time" value="18" /&gt;
      &lt;exchange-documents&gt;
        &lt;exchange-document country="US" number="8049504"&gt;
        ....
        ....
        ....

        &lt;/exchange-document&gt;
      &lt;/exchange-documents&gt;
</code></pre>

<p>I am parsing the XML and using XPath. In most of the XML files, the first line contains some text or spaces (refer the above xml)</p>

<p>Without that leading text, it parses successfully, but if any text appears it produces the below error:</p>

<blockquote>
<pre><code>--- exec-maven-plugin:1.2.1:exec (default-cli) @ XMLHandling ---
</code></pre>
  
  <p>[Fatal Error] :1:1: Content is not allowed in prolog.</p>
</blockquote>

<p>How can I get around this? </p>

<p>The code that I am using:</p>

<pre><code>public static void main(String[] args) throws ParseException {

        String filePath = "D:/newxml.xml";

        try {
            FileInputStream file = new FileInputStream(new File(filePath));
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
             Document xmlDocument = builder.parse(file);
            XPath xPath = XPathFactory.newInstance().newXPath();

            String pubOrPatentNumber = xPath.compile("//preference").evaluate(xmlDocument);
            ...
            ...
            }
            }
</code></pre>

<p>I can manually remove the text and execute, but I need to solve this within my code to clean up the input automatically.</p>