Question

I'm generating XML file dynamiquely from database using Ruby Builder (http://builder.rubyforge.org/).

To add a Tag which name is in a variable, I found this: xml.tag!(@myTagName)

How to dynamiquelly insert à list of attributes in this Tag ?

  • I dont know the names of the attributes which are loaded dynamically from database
  • I need to create a loop because I dont know how many attributes will be inserted.

Thanks.

Was it helpful?

Solution

I think it shouldn't be hard to do what you need. Whenever you add a tag you can pass along an optional hash which will be the tag's attributes. So for example if you do:

  builder = Builder::XmlMarkup.new
  xml = builder.person(name: "foo", age: 0 ) 

Then you will get <person name='foo' age='0'/>

So in a similar way if you build your dynamic attributes as a hash you can use the #tag! method like the following:

xml = builder.tag!(tag_name, attributes_hash)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top