我正在尝试解析Jenkins作业“ config.xml”文件。
这是我尝试使用HTTP :: Tiny的一种方法:
use HTTP::Tiny;
my $page = "http://localhost:8080/job/Job_One_Name/config.xml";
system("start $page"); # this loads the xml file in browser successfully
my $wsdlResponse = HTTP::Tiny->new->get($page);
$wsdlResponse->{success} or die print $logger->error_die($!);
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
它死于错误“错误的文件描述符”。
如果我注释掉该行,则脚本在parse_file行处中断,并显示以下错误消息:
Could not create file parser context for file "<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjo
b%2FJob_One_Name/config.xml'/><script>window.location.replace('/login?from=%2Fjob%2FJob_One_Name/config.xml');</script></head><body style='background-color:white; color:white;'>
Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->
</body></html>
": Result too large at Job.pm line 67.
我也从不同的示例在线尝试了XML :: Twig和LibXML-> load_xml和load_html,但是它们都死于类似于“无法为文件创建文件解析器上下文”,“错误的文件描述符”或“指定”的错误。位置,字符串或IO”。
关于可能是什么问题的任何想法?提前致谢。
更新:
遵循詹金斯(Jenkins)的“身份验证脚本客户端”中的“脚本客户端的Perl LWP示例” ,我已经成功地进行了身份验证:
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my %options = {'myUserName' => 'myPassword'};
$req = HTTP::Request->new( GET => $serverHost );
$req->authorization_basic( 'myUserName', 'myPassword' );
my $res = $uagent->request($req);
# Check the outcome of the response
print "Result: " . $res->status_line . "\n";
print $res->headers->as_string;
print "\n";
if ( !$res->is_success ) {
print "Failed\n";
} else {
print "Success!\n";
# Parse XML
my $wsdlResponse = HTTP::Tiny->new->get($page, \%options);
#$dom = XML::LibXML->load_xml(location=>$page);
#print $dom . "\n";
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
#print $res->content, "\n";
}
它仍然给我同样的“需要身份验证”错误。我不知道如何使用成功的授权请求来解析文件。
我想出了一种使用经过身份验证的请求输出结果的方法。由于请求输出xml内容以及标头响应,即:
HTTP/1.1 200 OK
Connection: close
Server: Jetty(8.y.z-SNAPSHOT)
Content-Type: application/xml
Client-Date: Wed, 30 Jul 2014 14:49:12 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1
<?xml version...
<project>
....
....
</project>
我使用正则表达式仅提取输出的xml部分并将其保存到文件中。从那里,我使用LibXML的parse_file方法来解析新写入的文件。(我有一个单独的子例程,该例程使用findnodes方法提取我想提取的特定节点。由于该解决方案处理了我遇到的身份验证问题,所以我没有发布它)。
解决方案:
use strict;
use warnings;
use XML::LibXML;
use Path::Class;
use LWP;
use HTTP::Cookies;
my $serverXmlUrl = "http\://localhost:8080/job/jobName/config.xml";
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my $request = HTTP::Request->new( GET => $serverXmlUrl );
$request->authorization_basic( 'UserName', 'Password' );
my $result = $uagent->request($request);
if ( !$result->is_success ) {
print "Failed\n";
} else {
print "Success!\n";
# Create new file to write to
my $dir = dir(".");
my $file = $dir->file("job.xml");
my $file_handle = $file->openw();
my $xml_content = $result->as_string; # full result output
# Regex to store only xml content
$xml_content =~ s/.*(<\?xml.*)/$1/s;
# Save xml_content to file
$file_handle->print($xml_content);
$file_handle->close();
# Parse newly written file
print "Parsing file... \n";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file);
print $doc;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句