如何使用 savon 嵌套属性!哈希?
我正在考虑使用 Ruby savon 来实现 SOAP。纯粹出于受虐的原因,我必须处理具有属性的 SOAP 元素。
所以,没问题,savon 文档网站上有一个示例强调了这种能力:
{ :person => "Eve", :attributes! => { :person => { :id => 666 } } }.to_soap_xml
"<person id=\"666\">Eve</person>"
我的问题是如何在子元素上设置属性,例如,假设我向 person 添加一个地址子元素:
{ :person => {:address => ""}, :attributes! => { :person => { :id => 666 } } }.to_soap_xml
现在我想添加一个地址元素的 id 属性:
如果我将地址嵌套在属性哈希中是行不通的:
{ :person => {:address => ""}, :attributes! => { :person => { :id => 666, :address => {:id => 44 }} }}.to_soap_xml
所以我的问题是,我怎样才能得到这个?
<person id=666><address id=44></address></person>
I'm looking at using Ruby savon for SOAP. For purely masochistic reasons I have to deal with SOAP elements having attributes.
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>"
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
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了之前的答案不再起作用的问题。最终我发现 https://github.com/savonrb/savon/issues/518 这引导我找到正确的答案现在添加属性的语法。
因此,前面的示例现在将完成,
这将生成以下 xml
I ran across the issue of the previous answer no longer working. Eventually I found https://github.com/savonrb/savon/issues/518 which lead me to the correct syntax to add attributes now.
So the previous example would now be done as
Which would generate the following xml
您很接近 - 只需将
:attributes!
键放入包含该值的同一哈希中。You were close - just needed to put the
:attributes!
key in the same hash that contains the value.