John John - 1 year ago 68
Perl Question

Perl soap request retrieving one value?

I have the following script that apparently this will not let me submit with more detail. It basically is pulling basic scheduling details from a SOAP request into XML and i need to retrieve a value from it.:

# 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='';
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

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

print $raw_data;

exit 0;

It outputs:

<?xml version='1.0' encoding='utf-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='' xmlns:xsd='' xmlns:xsi='' xmlns:SOAP-ENC=''>
<SOAP-ENV:Body><ns1:downloadResponse SOAP-ENV:encodingStyle='' 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>

<xtvd from='2016-09-27T00:00:00Z' to='2016-09-27T23:59:59Z' schemaVersion='1.3' xmlns='urn:TMSWebServices' xmlns:xsi='' xsi:schemaLocation='urn:TMSWebServices'>

</SOAP-ENV:Envelope> [

I strictly want to date/time of the part that says:

Your subscription will expire: 2017-09-29T21:18:20Z

How do I do that?


Answer Source

The most pragmatic way would be to just take the raw result that contains the XML as a string, and parse it with a regular expression. All you want is a fixed pattern, so there is no need to actually use an XML parser (or the built-in stuff that SOAP::Lite brings).

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

Now you have 2017-09-29T21:18:20Z inside of $date_string and can do with it what you want.