2

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

dwarring
  • 4,794
  • 1
  • 26
  • 38

1 Answers1

2

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

Kim Ryan
  • 515
  • 1
  • 3
  • 11
  • Kim, thanks, this worked as expected! I have changed the print $output line to this: print $output XMLout(\%keyHash,KeyAttr => [ ], RootName => 'response'); Now I have the output.xml populated with the XML but I still have one question and maybe you can help me, how to output instead of: ... Something like: AccountNumber ... CNTR0016716 ... ... Txs! – Felipe Bueno Barbosa Oct 29 '14 at 11:40
  • Hey, nevermind my previous comment, I just found out that I just needed to add the option NoAttr => 1 into the XMLOut(); and then the XML output worked as I expected - Thanks! – Felipe Bueno Barbosa Oct 29 '14 at 17:15