如何使用 savon 嵌套属性!哈希?

发布于 2024-10-06 14:20:40 字数 798 浏览 6 评论 0原文

我正在考虑使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-10-13 14:20:40

我遇到了之前的答案不再起作用的问题。最终我发现 https://github.com/savonrb/savon/issues/518 这引导我找到正确的答案现在添加属性的语法。

因此,前面的示例现在将完成,

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

这将生成以下 xml

<person id="666">
  <address id="44"/>
</person>

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

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

Which would generate the following xml

<person id="666">
  <address id="44"/>
</person>
扶醉桌前 2024-10-13 14:20:40

您很接近 - 只需将 :attributes! 键放入包含该值的同一哈希中。

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

# => "<person id=\"666\"><address id=\"44\"></address></person>"

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>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文