title: Content is not allowed in prolog, but prolog is fine at run-time? Groovy, Jenkins, Java, Pipeline, XML
body: <p>I am trying to get the version number from an XML file in a Jenkins pipeline. However, I keep getting the following error message  </p>
  
  <blockquote>
    <p>"org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;
    Content is not allowed in prolog."</p>
  </blockquote>
  
  <p>I have tried encoding using a different encoding. (UTF-8-BOM). I have also tried specifying the encoding that I am using when reading the XML file into the variable for parsing. I have also tried re-encoding the variable and putting it into another string. </p>
  
  <p>Here is my XML file.  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!--
  This is a nuspec. It mostly adheres to https://docs.nuget.org/create/Nuspec-Reference. Chocolatey uses a special version of NuGet.Core that allows us to do more than was initially possible. As such there are certain things to be aware of:
  * the package xmlns schema url may cause issues with nuget.exe
  * Any of the following elements can ONLY be used by choco tools - projectSourceUrl, docsUrl, mailingListUrl, bugTrackerUrl, packageSourceUrl, provides, conflicts, replaces 
  * nuget.exe can still install packages with those elements but they are ignored. Any authoring tools or commands will error on those elements 
  --&gt;
  &lt;!-- Do not remove this test for UTF-8: if “O” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. --&gt;
  &lt;package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"&gt;
    &lt;metadata&gt;
      &lt;!-- == PACKAGE SPECIFIC SECTION == --&gt;
      &lt;id&gt;Test-Package&lt;/id&gt;
      &lt;version&gt;18.5.0&lt;/version&gt;
      &lt;owners&gt;Conor&lt;/owners&gt;
      &lt;!-- ============================== --&gt;
      &lt;!-- == SOFTWARE SPECIFIC SECTION == --&gt;
      &lt;title&gt;test (Install)&lt;/title&gt;
      &lt;authors&gt;Conor&lt;/authors&gt;
      &lt;tags&gt;test package test-package&lt;/tags&gt;
      &lt;summary&gt;test package Jenkins&lt;/summary&gt;
      &lt;description&gt;Test package for CI.&lt;/description&gt;
      &lt;!-- =============================== --&gt;      
      &lt;!--&lt;dependencies&gt;
      &lt;/dependencies&gt;--&gt;
    &lt;/metadata&gt;
  &lt;/package&gt;
  </code></pre>
  
  <p>Here is the code that I am using to get the XML file and access the version. </p>
  
  <pre><code>strNuspec = "test.nuspec"  
  encoding = "UTF-8"  
  
  //We need to read the file and store the output within a variable we will then process that variable using the XMLParser.  
  
  echo 'Reading nuspec into string' 
  def xml = readFile file: "${strNuspec}", encoding: "${encoding}"
  println xml  
  
  
  //Attempting to flush the buffer due to having issues with prolog.
  println ""  
  
  
  //YOU CAN NOT PROCESS THE XML file directly!!! This is why we had to read the file into a string then process the string.  
  
  echo 'Extracting and printing the version number'   
  //Parsing the document with XML SLurper.  
  def xmlPackage = new XmlSlurper().parseText(xml)  
  
  //We start traversing the document from xmlPackage We can't use 'package' because it is a reserved keyword (Might give us complications).  
  
  def getVersion = xmlPackage.metadata.version
  
  //We have the instance of the version node, we want the text inside that node so we call the text() method.  
  
  def version = getVersion.text()  
  //Print the version so that we know we were successful.  
  
  println version
  </code></pre>
  
  <p>Value wtihin XML at run time. (Output using println)  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!--
  This is a nuspec. It mostly adheres to https://docs.nuget.org/create/Nuspec-Reference. Chocolatey uses a special version of NuGet.Core that allows us to do more than was initially possible. As such there are certain things to be aware of:
  * the package xmlns schema url may cause issues with nuget.exe
  * Any of the following elements can ONLY be used by choco tools - projectSourceUrl, docsUrl, mailingListUrl, bugTrackerUrl, packageSourceUrl, provides, conflicts, replaces 
  * nuget.exe can still install packages with those elements but they are ignored. Any authoring tools or commands will error on those elements 
  --&gt;
  &lt;!-- Do not remove this test for UTF-8: if “O” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. --&gt;
  &lt;package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"&gt;
    &lt;metadata&gt;
      &lt;!-- == PACKAGE SPECIFIC SECTION == --&gt;
      &lt;id&gt;Test-Package&lt;/id&gt;
      &lt;version&gt;18.5.0&lt;/version&gt;
      &lt;owners&gt;Conor&lt;/owners&gt;
      &lt;!-- ============================== --&gt;
      &lt;!-- == SOFTWARE SPECIFIC SECTION == --&gt;
      &lt;title&gt;test (Install)&lt;/title&gt;
      &lt;authors&gt;Conor&lt;/authors&gt;
      &lt;tags&gt;test package test-package&lt;/tags&gt;
      &lt;summary&gt;test package Jenkins&lt;/summary&gt;
      &lt;description&gt;Test package for CI.&lt;/description&gt;
      &lt;!-- =============================== --&gt;      
      &lt;!--&lt;dependencies&gt;
      &lt;/dependencies&gt;--&gt;
    &lt;/metadata&gt;
  &lt;/package&gt;
  </code></pre>
  
  <p>I expect to get the version and have it output to the screen.</p>