Adding attributes to SOAP-Lite envelope

There is a common theme in developing software while having to use badly designed libraries: you know exactly what you want to achieve, and are not asking for much, but a particular library prevents you from progressing. Without any doubt, SOAP::Lite in Perl is this kind of a library. Suppose we want to perform some simple modification of the generated XML SOAP message, such as adding another attribute to the Envelope element. This shouldn't be hard at all given the purpose of the library. Yet it turns out otherwise. Is there an officially blessed way of doing it with SOAP::Lite? I don't know. But there is this hack:

    $envelope_orig = \&SOAP::Serializer::envelope;
    *SOAP::Serializer::envelope = sub {
        my $str = $envelope_orig->(@_);
        $str =~ s{xmlns:xsi=}{xsi:schemaLocation="http://whatever" xmlns:xsi=}s;
        return $str;

By replacing the SOAP::Serialize::envelope subroutine you can alter the resulting XML message in whatever way you wish. This is working around the API, a potentially dangerous thing to do in general (though in this particular case, the danger of SOAP::Serialize::envelope being removed in a next version is slim). Use at your own risk.


Anonymous said...

great hack!

Fabio Lo Giudice said... i can use to modify envelope tag server side?

Post a Comment