Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add attribute to Nokogiri node?

Tags:

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!

like image 881
Yuval Karmi Avatar asked Sep 01 '10 01:09

Yuval Karmi


1 Answers

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

node['foo'] = 'bar' 

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

like image 149
Greg Campbell Avatar answered Oct 19 '22 23:10

Greg Campbell