Savon: Array of XML tags

不问归期 提交于 2019-12-24 04:54:10

问题


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?


回答1:


I have found the solution.

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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/7001957/savon-array-of-xml-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!