Efficient way to replace content of a few tags using Nokogiri
I have an RSS document that has a few tags, let's say named <foo开发者_C百科>
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 foo
and bar
elements no matter where they are located within the document:
doc = Nokogiri::XML(open("http://example.com/rss.xml"))
for element in doc.css('foo, bar')
element.content = "something"
end
You can also use multiple CSS selectors or XPath query, have a look at Nokogiri documentation:
- http://nokogiri.org
- http://nokogiri.org/Nokogiri/XML/Node.html#method-i-css
- http://nokogiri.org/Nokogiri/XML/Node.html#method-i-xpath
xml = "<r>
<foo>Hello<b>World</b></foo>
<x>It's <bar>Nice</bar> to see you.</x>
<foo>Here's another</foo>
<y>Don't touch me.</y>
</r>"
require 'nokogiri'
doc = Nokogiri::XML(xml)
doc.search('foo,bar').each do |node|
node.inner_html = "I am #{node.name} and I used to say #{node.text.inspect}"
end
puts doc
#=> <?xml version="1.0"?>
#=> <r>
#=> <foo>I am foo and I used to say "HelloWorld"</foo>
#=> <x>It's <bar>I am bar and I used to say "Nice"</bar> to see you.</x>
#=> <foo>I am foo and I used to say "Here's another"</foo>
#=> <y>Don't touch me.</y>
#=> </r>
You can also use doc.xpath('//foo|//bar')
to find all the foo
and bar
elements at any depth. (The CSS syntax is shorter and sufficiently powerful, though.)
In the future, you should supply an actual sample of the XML you are parsing, and an actual sample of the sort of transformation you wish to apply.
精彩评论