サンプル
require 'nokogiri'
count = 0
File.open("./source.xml", 'r') do |f|
xml = Nokogiri::XML(f)
item_nodes = xml.xpath('/xpath/to/contents')
item_nodes.each do |elem|
count += 1
end
end
puts count #=> ex. 100
-
Nokogiri::XML
にFileクラスのオブジェクトを渡す。 - 要素の探索はXPathを使う
- xpathメソッドの返り値は
Nokogiri::XML::NodeSet
のオブジェクトで、Enumerable
XPathの指定方法は
https://qiita.com/rllllho/items/cb1187cec0fb17fc650a
を参考にしました。
以上です。