我如何使用 savon 嵌套属性!哈希? [英] How do I use savon nested attributes! hash?
问题描述
我正在考虑将 Ruby savon 用于 SOAP.纯粹出于自虐的原因,我必须处理具有属性的 SOAP 元素.
I'm looking at using Ruby savon for SOAP. For purely masochistic reasons I have to deal with SOAP elements having attributes.
所以,没问题,savon docs 站点上有一个突出显示此功能的示例:
So, no problem, there is an example on the savon docs site which highlights this ability:
{ :person => "Eve", :attributes! => { :person => { :id => 666 } } }.to_soap_xml
"<person id=\"666\">Eve</person>"
我的问题是如何在子元素上设置属性,例如,假设我向person添加一个地址子元素:
My problem is how do I set attributes on child elements, for example, say I add an address child element to person:
{ :person => {:address => ""}, :attributes! => { :person => { :id => 666 } } }.to_soap_xml
现在我想给地址元素添加一个 id 属性:
Now I want to add an id attribute to the address element:
如果我在属性哈希中嵌套地址是不行的:
It's no go if I nest address in the attributes hash:
{ :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>
推荐答案
你已经很接近了 - 只需要将 :attributes!
键放在包含该值的同一个哈希中.
You were close - just needed to put the :attributes!
key in 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>"
这篇关于我如何使用 savon 嵌套属性!哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!