简体   繁体   English

如何使用XML API获取Jenkins职位列表

[英]How to get a list of Jenkins Jobs using XML API

I got the raw xml data from Jenkins REST API http://jenkins-host:8080/api/xml . 我从Jenkins REST API http://jenkins-host:8080/api/xml获得了原始xml数据。 Now I am working on getting the job name list out of this xml into a perl array or variable. 现在,我正在努力将工作名称列表从该xml中移出到perl数组或变量中。 following is the format of xml API 以下是xml API的格式

<hudson>
<job>
  <name>Test_Job1</name>
  <url>http://jenkins-host:8080/job/Test_job1/</url>
  <color>red</color>
</job>
<job>
  <name>Test_job2</name>
  <url>http://jenkins-host:8080/job/Test_job2/</url>
  <color>red</color>
</job>
<view>
  <name>Test_View</name>
  <url>http://jenkins-host:8080/</url>
</view>
</hudson>

Here I want to store Only the job names into an array not the view name. 在这里,我只想将作业名称存储到数组中,而不是视图名称。 Ex: 例如:

@list = (Test_job1, Test_job2)

With XML::Twig it would be: 使用XML :: Twig它将是:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

my @jobs;
XML::Twig->new( twig_roots => { 'job/name' => sub { push @jobs, $_->text; } })
         ->parseurl( 'http://jenkins-host:8080/api/xml');
my $xml = <<XML;
<hudson>
<job>
  <name>Test_Job1</name>
  <url>http://jenkins-host:8080/job/Test_job1/</url>
  <color>red</color>
</job>
<job>
  <name>Test_job2</name>
  <url>http://jenkins-host:8080/job/Test_job2/</url>
  <color>red</color>
</job>
<view>
  <name>Test_View</name>
  <url>http://jenkins-host:8080/</url>
</view>
</hudson>
XML

my @rules = (
  'hudson' => sub { $_[1]->{name} },
  job  => sub { '@name' => $_[1]{name} },
  name => 'content',
  _default => undef,
);
my $xr = XML::Rules->new(rules => \@rules);
my $data = $xr->parse($xml);

print Dumper $data;

Or: 要么:

my @jobs;
my @rules = (
  job  => sub { push @jobs, $_[1]{name} },
  name => 'content',
  _default => undef,
);
my $xr = XML::Rules->new(rules => \@rules);
$xr->parse($xml);

print Dumper \@jobs;

The simplest thing to do is this regular expression. 最简单的事情就是这个正则表达式。

my @matches = ( $xml =~ m(<name>(.*?)</name>)gs) ;

If the format of your XML is subject to frequent changes, then you'll want to consider an XML parser instead of this simple regex match. 如果您的XML格式经常更改,那么您将需要考虑使用XML解析器而不是这种简单的正则表达式匹配。


Edit: adding an explanation 编辑:添加说明

The regular expression assumes that you have all of the XML in one scalar variable, the 's' modifier tells Perl to treat entire string as one long line ('.' will match a newline), and the 'g' modifies tell Perl to search the entire string, rather than quitting on the first match. 正则表达式假定您将所有XML都放在一个标量变量中,“ s”修饰符告诉Perl将整个字符串视为一条长行(“。”将与换行符匹配),而“ g”修饰符告诉Perl以搜索整个字符串,而不是在第一个匹配项时退出。

The regex itself simply finds all name tag pairs, and captures what lives in between them. 正则表达式本身只是查找所有名称标签对,并捕获它们之间的内容。 by adding the question mark to modify the '. 通过添加问号来修改'。 ' pattern, we tell perl to be non-greedy, and so it stops capturing when it sees the first closing name tag. '模式,我们告诉perl不要贪心,因此当它看到第一个结束名称标签时,它将停止捕获。 otherwise '. 除此以外 '。 ' would match until the very last name closing tag and that is not what we want. '会一直匹配到最后一个名字结束标记,而这不是我们想要的。

We could also have written the capture as ([^<]+). 我们也可以将捕获内容写为([^ <] +)。 I suppose that's a matter of preference. 我想这是偏爱的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM