« Programming Web Services with Perl website | Main | DIME support is coming! »

Comments

Sandeep Satavlekar

I wrote a subroutine myself which does the above thing..It works just fine for me. I'd be glad to know if anyone notices any bugs. Here it is:

#########################################################################################
# Soapify
#
# Description:
# Given an arrayref (infinite depth), constructs a valid SOAP Object
#
# Parameters:
# $args: Arrayref
#
# Format of the arrayref:
# [xml_element_name, value, attributes]
# xml_element_name must be a scalar, value could be a scalar or another arrayref of the same format and
# attributes must be a hashref. xml_element_name and attributes are optional.
#
# Examples:
# 1. ["ElemName1", "ElemValue1", { "xmlns" => "http://blah/blah"}]
# 2. ["ElemName2", [
# ["NameAtLevel1", "ValueAtLevel1", {}],
# ], {"attrname" => "attrvalue"}]
# 3. [
# ["name", "value"]
# ]
#
# Return Value:
# SOAP object
#########################################################################################
sub Soapify {
my ($args) = (@_);

confess "The argument must be an rrayref!" if (ref($args) !~ /ARRAY/ );

my @namevaluearray = @$args;
my $soapobject = new SOAP::Data;

if (defined ($namevaluearray[0]) && ref($namevaluearray[0]) !~ /ARRAY/) {

#------------------name------------------------#
$soapobject->name($namevaluearray[0]);

#------------------value-----------------------#
if (defined $namevaluearray[1] && !ref($namevaluearray[1])) {
$soapobject->value($namevaluearray[1]);
}
elsif (ref($namevaluearray[1]) =~ /ARRAY/) {
my $soapvalue = Soapify($namevaluearray[1]);
my @pass = (ref($soapvalue) =~ /ARRAY/) ? @$soapvalue : ($soapvalue);
$soapobject->value(\SOAP::Data->value(@pass));
}

#-------------------attribute------------------#
my $attr = $namevaluearray[2];
if (ref($attr) =~ /HASH/) {
$soapobject->attr($attr);
}
}
elsif (ref($namevaluearray[0]) =~ /ARRAY/) {
my @valuesarray;
foreach my $element (@namevaluearray) {
my $reftoelement = (ref($element) =~ /ARRAY/) ? $element : [$element];
push(@valuesarray, Soapify($reftoelement));
}
return [@valuesarray];
}

return $soapobject;
}

Sandeep Satavlekar

ohh god! the entire indentation is lost. But I hope it'd still be straightforward to understand.

Rgds,
Sandeep Satavlekar

The comments to this entry are closed.