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?
I have found the solution.
soap.body = {
#... other tags
"content" => {
"item" => ["", "", ""],
:attributes! => {
"item" => {
"a" => ["1", "2", "3"],
"b" => ["0", "0", "0"]
}
}
}
#... other tags
}
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
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
精彩评论