Loading ...
Sorry, an error occurred while loading the content.

Re: [soaplite] Constructing dynamic number of elements with SOAP::Data

Expand Messages
  • Paul Kulchenko
    Hi, Byrne! ... you have in @array1: [ page , {...}, page , {...}] which is serialized EXACTLY as specified: as array of those elements. You can generate
    Message 1 of 5 , Mar 25, 2002
    • 0 Attachment
      Hi, Byrne!

      > Naturally, the code above does not work - I am still hacking around
      > trying to get figure it out. :/
      It doesn't work simpy because by the time you do this:

      > %hash1 = ( "page" => { "url" => "foo", "hits" => 5 } );
      > %hash2 = ( "page" => { "url" => "bar", "hits" => 15 } );
      > @array1 = [ %hash1, %hash2 ];

      you have in @array1: ['page', {...}, 'page', {...}]

      which is serialized EXACTLY as specified: as array of those elements.


      You can generate pretty much everything you need using SOAP::Data
      syntax. There is only one thing to keep in mind when using one
      SOAP::Data object inside another one: SOAP::Data is a modifier, it's
      not necessarily generates independent XML element.

      SOAP::Data->name(foo => SOAP::Data->name(bar => 1))

      generates:

      <bar xsi:type="xsd:int">1</bar>

      which may not be what you expect, whereas

      SOAP::Data->name(foo => \SOAP::Data->name(bar => 1))

      generates this:

      <foo><bar xsi:type="xsd:int">1</bar></foo>

      That's also true for more than one element as a value:

      SOAP::Data->name(foo => \SOAP::Data->name(bar => 1, 2, 3))

      generates this:

      <foo>
      <bar xsi:type="xsd:int">1</bar>
      <bar xsi:type="xsd:int">2</bar>
      <bar xsi:type="xsd:int">3</bar>
      </foo>

      Hope it helps.

      Best wishes, Paul.

      --- Byrne Reese <breese@...> wrote:
      > I think what Doug is asking is how to serialize a struct like this:
      >
      > %hash1 = ( "page" => { "url" => "foo", "hits" => 5 } );
      > %hash2 = ( "page" => { "url" => "bar", "hits" => 15 } );
      > @array1 = [ %hash1, %hash2 ];
      >
      > into the proper XML. In an ideal case, one should just pass in a
      > variable like:
      >
      > print SOAP::Serializer
      > ->maptype({SOAPStruct} => ''})
      > ->readable(1)
      > ->method("something" => SOAP::Data->name("pages" => @array1) );
      >
      > Naturally, the code above does not work - I am still hacking around
      > trying to get figure it out. :/
      >
      > On Mon, 2002-03-25 at 14:37, Paul Kulchenko wrote:
      > > Hi, Doug!
      > >
      > > Something like this should work:
      > >
      > > use SOAP::Lite;
      > >
      > > #1
      > >
      > > my $opt1 = SOAP::Data->name(pages => \SOAP::Data->value(
      > > SOAP::Data->name(page => {url => "blah", hits => 81})
      > > ));
      > >
      > > print SOAP::Serializer->maptype({SOAPStruct => ''})->readable(1)
      > > ->method(something => $opt1);
      > >
      > > #2
      > >
      > > my @values = map { SOAP::Data->name(page => $_) }
      > > {url => "blah", hits => 81},
      > > {url => "foo", hits => 181};
      > >
      > > my $opt2 = SOAP::Data->name(pages =>
      > \SOAP::Data->value(@values));
      > >
      > > print SOAP::Serializer->maptype({SOAPStruct => ''})->readable(1)
      > > ->method(something => $opt2);
      > >
      > > Let me know if it doesn't work for you for some reason.
      > >
      > > Best wishes, Paul.
      > >
      > > --- Doug Fulton <dfulton@...> wrote:
      > > > I am trying to construct the following as part of my request:
      > > >
      > > > <pages>
      > > > <page> <url>blah</url> <hits>88</hits></page>
      > > > <page> <url>deeblah</url> <hits>65</hits></page>
      > > > ...
      > > > </pages>
      > > >
      > > > I don't know in advance how many page elements I will have.
      > Advice
      > > > on
      > > > how to do this with SOAP::Data (or otherwise)? My search for
      > > > similar
      > > > examples has come up empty. soapenv.org has a nice example of
      > > > nested
      > > > elements, but not in a dynamic context.
      > > >
      > > > Cheers,
      > > > Doug
      > > >
      > > >
      > > >
      > > >
      > > > ------------------------ Yahoo! Groups Sponsor
      > > >
      > > > To unsubscribe from this group, send an email to:
      > > > soaplite-unsubscribe@yahoogroups.com
      > > >
      > > >
      > > >
      > > > Your use of Yahoo! Groups is subject to
      > > > http://docs.yahoo.com/info/terms/
      > > >
      > > >
      > >
      > >
      > > __________________________________________________
      > > Do You Yahoo!?
      > > Yahoo! Movies - coverage of the 74th Academy Awards�
      > > http://movies.yahoo.com/
      > >
      > > ------------------------ Yahoo! Groups Sponsor
      > >
      > > To unsubscribe from this group, send an email to:
      > > soaplite-unsubscribe@yahoogroups.com
      > >
      > >
      > >
      > > Your use of Yahoo! Groups is subject to
      > http://docs.yahoo.com/info/terms/
      > >
      > --
      > :/ byrne
      >
      > breese@...
      > (415) 544-7732


      __________________________________________________
      Do You Yahoo!?
      Yahoo! Movies - coverage of the 74th Academy Awards�
      http://movies.yahoo.com/
    • Doug Fulton
      Brilliant. Now, after building up my dynamic hash, say %pages = ( a ,1, b ,2, c ,3); I traverse it and push resulting SOAP::Data objects into @values: while
      Message 2 of 5 , Mar 25, 2002
      • 0 Attachment
        Brilliant.

        Now, after building up my dynamic hash, say

        %pages = ("a",1,"b",2,"c",3);

        I traverse it and push resulting SOAP::Data objects into @values:

        while (($key, $value) = each(%pages)) {
        push @values, SOAP::Data->name(page => {url => $key, hits =>
        $value});
        }

        Then I use @values as you indicated.

        my $opt2 = SOAP::Data->name(pages => \SOAP::Data->value(@values));

        It works like a charm. Simple, really. But as they say, before you
        understand something, it seems impossible and once you understand it, it
        seems trivial.

        Thanks much!!
        Doug

        On Monday, March 25, 2002, at 05:37 PM, Paul Kulchenko wrote:

        > Hi, Doug!
        >
        > Something like this should work:
        >
        > use SOAP::Lite;
        >
        > #1
        >
        > my $opt1 = SOAP::Data->name(pages => \SOAP::Data->value(
        > SOAP::Data->name(page => {url => "blah", hits => 81})
        > ));
        >
        > print SOAP::Serializer->maptype({SOAPStruct => ''})->readable(1)
        > ->method(something => $opt1);
        >
        > #2
        >
        > my @values = map { SOAP::Data->name(page => $_) }
        > {url => "blah", hits => 81},
        > {url => "foo", hits => 181};
        >
        > my $opt2 = SOAP::Data->name(pages => \SOAP::Data->value(@values));
        >
        > print SOAP::Serializer->maptype({SOAPStruct => ''})->readable(1)
        > ->method(something => $opt2);
        >
        > Let me know if it doesn't work for you for some reason.
        >
        > Best wishes, Paul.
        >
        > --- Doug Fulton <dfulton@...> wrote:
        >> I am trying to construct the following as part of my request:
        >>
        >> <pages>
        >> <page> <url>blah</url> <hits>88</hits></page>
        >> <page> <url>deeblah</url> <hits>65</hits></page>
        >> ...
        >> </pages>
        >>
        >> I don't know in advance how many page elements I will have. Advice
        >> on
        >> how to do this with SOAP::Data (or otherwise)? My search for
        >> similar
        >> examples has come up empty. soapenv.org has a nice example of
        >> nested
        >> elements, but not in a dynamic context.
        >>
        >> Cheers,
        >> Doug
        >>
        >>
        >>
        >>
        >> ------------------------ Yahoo! Groups Sponsor
        >>
        >> To unsubscribe from this group, send an email to:
        >> soaplite-unsubscribe@yahoogroups.com
        >>
        >>
        >>
        >> Your use of Yahoo! Groups is subject to
        >> http://docs.yahoo.com/info/terms/
        >>
        >>
        >
        >
        > __________________________________________________
        > Do You Yahoo!?
        > Yahoo! Movies - coverage of the 74th Academy AwardsÆ
        > http://movies.yahoo.com/
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.