2

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.

1 Answer 1

7

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.