2

我将Savon用于 SOAP 请求,并且在 SOAP 请求 XML 的某个位置,我需要生成这段代码:

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

最好的方法是什么?

4

3 回答 3

3

我找到了解决方案。

soap.body = {  
    #... other tags  
    "content" => {  
        "item" => ["", "", ""],  
        :attributes! => {  
            "item" => {  
                "a" => ["1", "2", "3"],  
                "b" => ["0", "0", "0"]  
            }  
        }  
    }
    #... other tags    
}  
于 2011-08-10T17:12:46.243 回答
1

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
于 2011-08-25T13:57:44.977 回答
0

您可以执行以下操作:

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
于 2011-08-09T22:07:07.663 回答