0

I am trying to get the version number from an XML file in a Jenkins pipeline. However, I keep getting the following error message

"org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog."

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.

Here is my XML file.

<?xml version="1.0" encoding="UTF-8"?>
<!--
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 
-->
<!-- 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. -->
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
  <metadata>
    <!-- == PACKAGE SPECIFIC SECTION == -->
    <id>Test-Package</id>
    <version>18.5.0</version>
    <owners>Conor</owners>
    <!-- ============================== -->
    <!-- == SOFTWARE SPECIFIC SECTION == -->
    <title>test (Install)</title>
    <authors>Conor</authors>
    <tags>test package test-package</tags>
    <summary>test package Jenkins</summary>
    <description>Test package for CI.</description>
    <!-- =============================== -->      
    <!--<dependencies>
    </dependencies>-->
  </metadata>
</package>

Here is the code that I am using to get the XML file and access the version.

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

Value wtihin XML at run time. (Output using println)

<?xml version="1.0" encoding="UTF-8"?>
<!--
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 
-->
<!-- 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. -->
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
  <metadata>
    <!-- == PACKAGE SPECIFIC SECTION == -->
    <id>Test-Package</id>
    <version>18.5.0</version>
    <owners>Conor</owners>
    <!-- ============================== -->
    <!-- == SOFTWARE SPECIFIC SECTION == -->
    <title>test (Install)</title>
    <authors>Conor</authors>
    <tags>test package test-package</tags>
    <summary>test package Jenkins</summary>
    <description>Test package for CI.</description>
    <!-- =============================== -->      
    <!--<dependencies>
    </dependencies>-->
  </metadata>
</package>

I expect to get the version and have it output to the screen.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
Conor
  • 11
  • 4

1 Answers1

-1

XmlSlurper isn't serializable so Jenkins doesn't cope with very well. However, if your XML structure is simple enough (i.e. a tag opens and closes on the same line and each tag has a unique name) and you only need the version text you can use a small regex instead:

def xml = readFile file: "${strNuspec}", encoding: "${encoding}"

def getXmlInnerText = { attribute ->
  def matcher = xml =~ "<$attribute>(.+)</$attribute>"
  matcher ? matcher[0][1] : null
}

def version = getXmlInnerText("version")
println version
towel
  • 2,094
  • 1
  • 20
  • 30
  • I need to use a XML parser to also manipulate the version within the future. But thank you regardless. – Conor Jun 05 '19 at 10:46