Blog An exploration of the art and
craft of software development

Mapping Soap Response without wsdl

Posted by Marty Haught on Friday, August 04, 2006
So I've been wrestling with trying to map a Soap4R response to Ruby classes. While I got really close I couldn't quite get this Account object to map an array of attributes (the a element below). Here's a snippet of the xml response:

       <CreateAccountResponse xmlns="urn:zimbraAdmin">
            <account name="" id="19832977-01ae-4290-a753-bc78c82196da">
                <a n="zimbraPrefMailPollingInterval">5m</a>
                <a n="zimbraContactMaxNumEntries">0</a>
                <a n="zimbraPrefGroupMailBy">conversation</a>
 <snipped a ton of additional 'a' elements>
Though I suspect there are plenty more inner workings of Soap4R that would make this easier I've come to the easiest solution for accessing this data. Instead of trying to map a hand-coded class I just let it come back as a soap mapping object. This works for most things but in this case the array of 'a' elements are all strings. The strings are just the value of the 'a' element tag. The vital piece, the 'n' attribute, seems to be missing... or hidden. It seems that Soap4R modifies the value's String class to have an __xmlattr attribute. Look at the following yaml output of the account object:

  !ruby/object:XSD::QName ?
    name: id
  : 19832977-01ae-4290-a753-bc78c82196da

  !ruby/object:XSD::QName ?
    name: name

- !str
  str: 5m
    !ruby/object:XSD::QName ?
      name: n
    : zimbraPrefMailPollingInterval
Curious, but if you do an inspect you won't see these hidden nuggets. In this particular case I was able to write this converter method to pull these out and make them easier to get to:

      if response.account
        acct = response.account
        class << acct
          attr_accessor :config, :id, :name   
        # Iterate over user properties/config and add to the config hash
        acct.config = Hash[* do |value|
          [value.__xmlattr[, 'n')],]
        # Populate our id and name attributes = acct.__xmlattr[,"id")] = acct.__xmlattr[,"name")]
        @account = acct
I'd still prefer to map these objects correctly so that I don't need to run code like this. However, this is an acceptible solution for now.
blog comments powered by Disqus