views:

86

answers:

1

I'm trying to add an attribute to an existing Nokogiri node. What I've done is this:

node.attributes['foobar'] = Nokogiri::XML::Attr.new('foo', 'bar')

But I get the error:

TypeError Exception: wrong argument type String (expected Data)

What is a Data data type, and how do I add an attribute to the Nokogiri object?

Thanks!

+3  A: 

I believe you should just need to use the []= method, i.e.

node['foo'] = 'bar'

You could also use node.set_attribute('foo', 'bar').

Greg Campbell
thanks, but that doesn't work. any other ideas?
yuval
This is what the Nokogiri docs say to do. Are you sure your `node` object is actually a `Nokogiri::XML::Element`? What was the error you got when you did `node['foo'] = 'bar'`?
wuputah
actually, you're right - that was a mistake on my part.greg - do you mind hitting edit and submit on your question so i can upvote it? thanks!
yuval
Done. I also linked to the RDoc for the method in question on Nokogiri::XML::Node.
Greg Campbell
Thanks! Upvoted and selected as the right answer :)
yuval