Search code examples
web-servicesperlsoapsoaplite

How to print XML provided by SOAP Webservice using Perl


I am completely new to Perl and I need to create a tool that will consume a SOAP Webservice and I need to save the XML provided by this WS in an output file. At this point I can consume the webservice and save it as hash data, but I need it to be in XML format.

My code is pretty simple and goes like this:

#!/usr/bin/perl -w
use SOAP::Lite ( +trace => "all", maptype => {} );
use IO::File;
use Data::Dump "pp";
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
   return 'username' => 'password';
}
my $soap = SOAP::Lite
    -> proxy('https://.../WebService.do?SOAP', ssl_opts => [     SSL_verify_mode => 0 ] ); 
my $method = SOAP::Data->name('execute') -> attr({xmlns => 'http://.../'});
my $output = IO::File->new(">output.xml");
my %keyHash = %{ $soap->call($method)->body};
print $output pp({%keyHash});
$output->close();

As I have the trace full on, I can see the XML that the webservice provide in the console while my program is executed, but, when it gets printed in the output file, I see the hash as defined in Perl, with the pairs of key => values, organized just like if it was a json:

{
  Docs => {
    AssetDefinition => "AccountNumber",
    BatchId => 1,
    Doc => [
      {
        AssetDefinitionId => "CNTR0016716",
        DateForRetention => "",
        FileName => "",
        FilePath => "",
        SequenceNumber => "",
      },
    ],
  },
}

The data is completely correct, but I needed it saved in the file as XML and at this point I think I am going in the wrong direction.

Any help will be fairly appreciated.

Thanks and regards,

Felipe


Solution

  • You are on the right track. The soap call just returns a perl data structure, a hash of hashes. You need an additional step to convert it to XML.

    I would recommedn this module http://search.cpan.org/~grantm/XML-Simple-2.20/lib/XML/Simple.pm

    use XML::Simple qw(:strict);
    my $xml = XMLout(\%keyHash);
    

    You can supply options to give more control over the XML formatting