Question

I want to edit a node of each item of an RSS feed in Ruby with Nokogiri and XPath.

I can get the value of this node but I can not edit them:

doc = Nokogiri::XML(open("http://www.pcinpact.com/rss/news.xml"))

doc.xpath('//item').each do |i|
  pp i.xpath('title').first.text
end

I get the value of the title node in each item node. I want to edit the "content" but I can't find how with xpath. Obviously I want to get my original XML with the modifications.

Any idea?

Was it helpful?

Solution

For setting the content use the content= method.

doc = Nokogiri::XML(open("http://www.pcinpact.com/rss/news.xml"))

doc.xpath('//item').each do |i|
  pp i.xpath('title').first.content = "My new title"
end

For more on how to manipulate a document in Nokogiri, check out "Modifying an HTML / XML Document".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top