How to use the nested attributes of savona! hash?

I am considering using Ruby savon for SOAP. For purely masochistic reasons, I have to deal with SOAP elements that have attributes.

So, there is no problem, there is an example on the doc files website sacon, which emphasizes this ability:

{ :person => "Eve", :attributes! => { :person => { :id => 666 } } }.to_soap_xml "<person id=\"666\">Eve</person>" 

My problem is how to set attributes for child elements, for example, let's say I add a child address element to a person:

 { :person => {:address => ""}, :attributes! => { :person => { :id => 666 } } }.to_soap_xml 

Now I want to add the id attribute to the address element:

It is not necessary if I put the address in the hash of the attributes:

 { :person => {:address => ""}, :attributes! => { :person => { :id => 666, :address => {:id => 44 }} }}.to_soap_xml 

So my question is: how can I get this?

 <person id=666><address id=44></address></person> 
+6
soap ruby savon
source share
2 answers

You were close - you just had to put the key :attributes! to the same hash that contains the value.

 { :person => { :address => "", :attributes! => { :address => { :id => 44 } } }, :attributes! => { :person => { :id => 666 } } }.to_soap_xml # => "<person id=\"666\"><address id=\"44\"></address></person>" 
+14
source share

I came across the question that the previous answer no longer works. In the end, I found https://github.com/savonrb/savon/issues/518 that led me to the correct syntax for adding attributes.

So the previous example will now execute as

 { :person => { :@id => 666, :address => { :@id => 44 } } } 

To create the next xml

 <person id="666"> <address id="44"/> </person> 
+15
source share

All Articles