I have an RSS document that has a few tags, let’s say named <foo> and <bar>, where I want to replace/massage the content. What’s the most efficient way of doing this? Do I parse the entire feed and replace content inline? If so, how would the block look like if I want to do it for the two sibling nodes above?
Does it require parsing the document sequentially and creating a new one as I go through content?
The document is getting created with something like:
doc = Nokogiri::XML(open("http://example.com/rss.xml"))
What’s the best way to iterate over doc and modify the contents of <foo> and <bar> from that point?
You can edit XML document directly in memory. If you’re looking for the simple way how to do it, you can use CSS selectors. Following code will change content of
fooandbarelements no matter where they are located within the document:You can also use multiple CSS selectors or XPath query, have a look at Nokogiri documentation: