序言中不允许内容,但序言在运行时可以吗? Groovy,Jenkins,Java,管道,XML [重复]
Posted
技术标签:
【中文标题】序言中不允许内容,但序言在运行时可以吗? Groovy,Jenkins,Java,管道,XML [重复]【英文标题】:Content is not allowed in prolog, but prolog is fine at run-time? Groovy, Jenkins, Java, Pipeline, XML [duplicate] 【发布时间】:2019-10-20 18:08:54 【问题描述】:我正在尝试从 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>
我希望获得版本并将其输出到屏幕上。
【问题讨论】:
我相信你在 xml 中有一些前缀,比如 BOM。解析器不允许这样做。 这是我最初的想法,但是在指定编码后前缀丢失了? 【参考方案1】:XmlSlurper
不可序列化,因此 Jenkins 不能很好地应对。但是,如果您的 XML 结构足够简单(即标签在同一行打开和关闭,并且每个标签都有一个唯一的名称)并且您只需要版本文本,您可以使用一个小的正则表达式:
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
【讨论】:
我需要使用 XML 解析器来操作未来的版本。不过还是谢谢你。以上是关于序言中不允许内容,但序言在运行时可以吗? Groovy,Jenkins,Java,管道,XML [重复]的主要内容,如果未能解决你的问题,请参考以下文章