How to create a dynamic tag using Nokogiri :: XML :: Builder?

I iterate over the set of tag names in the array, and I want to print each of them using the builder without resorting to the XML manual "<<Method.

I thought that:

builder = Nokogiri::XML::Builder.new do |xml| for tag in tags xml.tag! tag, someval end end 

will do this, but it just creates tags called "tag" and puts the tag variable in the text value of the element.

Can anyone help? It seems that this should be relatively simple, I just could not find the answer to the search engines. I probably do not ask the right question.

+4
source share
2 answers

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

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

try using the missing method

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

Source: https://habr.com/ru/post/1344783/


All Articles