Savon:XML 标签数组

发布于 2024-11-28 18:09:30 字数 291 浏览 2 评论 0原文

我正在使用 Savon 进行 SOAP 请求,并且在 SOAP 请求 XML 的某些位置,我需要生成这个一段代码:

<content>
  <item a="1" b="0"/>
  <item a="2" b="0"/>
  <item a="3" b="0"/>
</content>

执行此操作的最佳方法是什么?

I'm using Savon for SOAP requests and in some place of the SOAP request XML, I need to generate this piece of code:

<content>
  <item a="1" b="0"/>
  <item a="2" b="0"/>
  <item a="3" b="0"/>
</content>

What's the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

花期渐远 2024-12-05 18:09:30

我已经找到了解决方案。

soap.body = {  
    #... other tags  
    "content" => {  
        "item" => ["", "", ""],  
        :attributes! => {  
            "item" => {  
                "a" => ["1", "2", "3"],  
                "b" => ["0", "0", "0"]  
            }  
        }  
    }
    #... other tags    
}  

I have found the solution.

soap.body = {  
    #... other tags  
    "content" => {  
        "item" => ["", "", ""],  
        :attributes! => {  
            "item" => {  
                "a" => ["1", "2", "3"],  
                "b" => ["0", "0", "0"]  
            }  
        }  
    }
    #... other tags    
}  
山有枢 2024-12-05 18:09:30

Savon v0.9.7 改进了对 Builder 的支持,我建议使用它而不是尝试通过哈希强制属性,因为它更具可读性:

soap.body do |xml|
  xml.content do
    xml.item(:a => "1", :b => "0")
    xml.item(:a => "2", :b => "0")
    xml.item(:a => "3", :b => "0")
  end
end

Savon v0.9.7 comes with improved support for Builder and I would suggest to use it instead of trying to force attributes via Hashes, because it's way more readable:

soap.body do |xml|
  xml.content do
    xml.item(:a => "1", :b => "0")
    xml.item(:a => "2", :b => "0")
    xml.item(:a => "3", :b => "0")
  end
end
岁吢 2024-12-05 18:09:30

你可以这样做:

def content
  xml = Builder::XmlMarkup.new
  xml.content do
    xml.item(:a => "1", :b => "0")
    xml.item(:a => "2", :b => "0")
    xml.item(:a => "3", :b => "0")
  end
end

You could do something like:

def content
  xml = Builder::XmlMarkup.new
  xml.content do
    xml.item(:a => "1", :b => "0")
    xml.item(:a => "2", :b => "0")
    xml.item(:a => "3", :b => "0")
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文