breaking into the unknown…

undefined method `content=’ for nokogiri

Leave a comment

while, traversing the xml file and retrieving value of one of the node , I was getting this error :  undefined method `content=’ for #<Nokogiri::XML::NodeSet:0xc0755b0>. Actually, I want to modify the node value on run time. The idea is to retrieve the node value, manipulate it and reinsert it.

My xml structure is as below

I want to modify the loanId value to append current_user id to it. I   used the below code
                      doc.xpath(“//list/request”).each do |doc|
                                 loan_number = doc.xpath(“loanId”).text
                                doc.xpath(“loanId”).content = loan_number +
                                puts doc.xpath(“id”).text

But, the code is throwing below error

undefined method `content=’ for #<Nokogiri::XML::NodeSet:0xc0755b0>

actually , xpath return an array of element, and so the content method ,  is called for array rather then the node. So, we should use at_xpath
and change the culprit line as below

doc.at_xpath(“loanId”).content = loan_number +
This will work now

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s