当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - Edit RSS feed with Nokogiri and XPath?

问题描述:

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?

网友答案:

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".

分享给朋友:
您可能感兴趣的文章:
随机阅读: