How to add attribute to Nokogiri node?

☆樱花仙子☆ 提交于 2019-12-05 09:53:57

问题


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!


回答1:


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

node['foo'] = 'bar'

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



来源:https://stackoverflow.com/questions/3614458/how-to-add-attribute-to-nokogiri-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!