Perl soap请求检索一个值?

时间:2016-09-27 14:57:45

标签: perl soap

我有以下脚本,显然这不会让我提交更多细节。它基本上是将基本调度细节从SOAP请求拉到XML中,我需要从中检索一个值。:

#!perl
#
# Quick sample script to fetch data from Tribune's Data Direct Service
#
# R. Eden 2/23/08
# (modified from XMLTV's tv_grab_na_dd, which started with a sample)
# script provided by Tribune

use SOAP::Lite;
use strict;

my $USER='username';
my $PASS='password';
my $START='2016-09-27T00:00:00Z';
my $STOP ='2016-09-27T23:59:59Z';

#
# Set login credientials
#
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
return lc($USER) => "$PASS";
}


#
# Deifne SOAP service
#
my $dd_service='http://dd.schedulesdirect.org/tech/tmsdatadirect/schedulesdirect/tvDataDelivery.wsdl';
my $proxy='http://localhost/';
my $soap= SOAP::Lite
       -> service($dd_service)
    -> outputxml('true')
    -> proxy($proxy, options => {compress_threshold => 10000,
                                 timeout            => 420});

#
# It's polite to set an agent string
#
$soap->transport->agent("perl/$0");


#
# Now let's get our data
#
my $raw_data=$soap->download($START,$STOP);


print $raw_data;

exit 0;

输出:

<?xml version='1.0' encoding='utf-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'>
<SOAP-ENV:Body><ns1:downloadResponse SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:ns1='urn:TMSWebServices'>
<xtvdResponse xsi:type='ns1:xtvdResponse'>

<messages xsi:type='ns1:messages'>

<message>Your subscription will expire: 2017-09-29T21:18:20Z</message>

</messages>
<xtvd from='2016-09-27T00:00:00Z' to='2016-09-27T23:59:59Z' schemaVersion='1.3' xmlns='urn:TMSWebServices' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='urn:TMSWebServices http://dd.schedulesdirect.org/tech/xml/schemas/tmsxtvd.xsd'>

<stations>
</stations>
<lineups>
</lineups>
</xtvd>
</xtvdResponse>
</ns1:downloadResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope> [

我严格要求<message>部分的日期/时间说明:

<message>Your subscription will expire: 2017-09-29T21:18:20Z</message>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

最实用的方法是将包含XML的原始结果作为字符串,并使用正则表达式解析它。你想要的只是一个固定的模式,所以不需要实际使用XML解析器(或SOAP :: Lite带来的内置东西)。

$raw_data =~ m/Your subscription will expire: ([^<]+)</;
my $date_string = $1;

现在2017-09-29T21:18:20Z内有$date_string,可以随心所欲地使用它。