序言中不允许内容,但序言在运行时是否正常? Groovy,Jenkins,Java,管道,XML

时间:2019-06-05 08:39:12

标签: xml jenkins groovy jenkins-pipeline jenkins-groovy

我正在尝试从Jenkins管道中的XML文件获取版本号。但是,我不断收到以下错误消息

  

“ org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;   序言中不允许包含内容。”

我尝试使用其他编码进行编码。 (UTF-8-BOM)。我也尝试过指定将XML文件读入变量进行解析时使用的编码。我也尝试过重新编码变量并将其放入另一个字符串中。

这是我的XML文件。

<?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>

这是我用来获取XML文件并访问版本的代码。

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

在运行时使用XML值。 (使用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>

我希望获得该版本并将其输出到屏幕上。

1 个答案:

答案 0 :(得分:-1)

select name, log_reuse_wait,log_reuse_wait_desc from sys.databases; 不可序列化,因此Jenkins不能很好地应对。但是,如果您的XML结构足够简单(即一个标签在同一行上打开和关闭,并且每个标签都有唯一的名称),而您只需要版本文本,则可以使用一个小的正则表达式来代替:

XmlSlurper