开发者

How can I generate a dynamic tag with Nokogiri::XML::Builder?

开发者 https://www.devze.com 2023-02-19 03:49 出处:网络
I am looping through a set of tag names in an array, and I want to print each one using builder without resorting to the manual XML of the \"<<\" method.

I am looping through a set of tag names in an array, and I want to print each one using builder without resorting to the manual XML of the "<<" method.

I thought that:

builder = Nokogiri::XML::Builder.new do |xml|

  for tag in tags
    xml.tag! tag, someval
  end
end

would do it, but it just creates tags with the name "tag", and puts the tag variable as the text value of the element.

Can anyone help? This seems like it should be relatively simple, I have just had t开发者_运维技巧rouble finding the answer on search engines. I am probably not asking the question the right way.


Try the following. I added a root node as Nokogiri requires one if I'm not mistaken.

builder = Nokogiri::XML::Builder.new do |xml|
  xml.root do |root|
    for tag in tags
      xml.send(tag, someval)
    end
  end
end


try to use method_missing

 builder = Nokogiri::XML::Builder.new do |xml|
   for tag in tags
     xml.method_missing(tag, someval)
   end
 end
0

精彩评论

暂无评论...
验证码 换一张
取 消