Parsing Large XML files w/ Ruby & Nokogiri

Skyline36

New Member
I have a large XML file (about 10K rows) I need to parse regularly that is in this format:\[code\]<summarysection> <totalcount>10000</totalcount></summarysection><items> <item> <cat>Category</cat> <name>Name 1</name> <value>Val 1</value> </item> ...... 10,000 more times</items>\[/code\]What I'd like to do is parse each of the individual nodes using nokogiri to count the amount of items in one category. Then, I'd like to subtract that number from the total_count to get an ouput that reads "Count of Interest_Category: n, Count of All Else: z".This is my code now:\[code\]#!/usr/bin/rubyrequire 'rubygems'require 'nokogiri'require 'open-uri'icount = 0 xmlfeed = Nokogiri::XML(open("/path/to/file/all.xml"))all_items = xmlfeed.xpath("//items") all_items.each do |adv| if (adv.children.filter("cat").first.child.inner_text.include? "partofcatname") icount = icount + 1 end endothercount = xmlfeed.xpath("//totalcount").inner_text.to_i - icount puts icountputs othercount\[/code\]This seems to work, but is very slow! I'm talking more than 10 minutes for 10,000 items. Is there a better way to do this? Am I doing something in a less than optimal fashion?
 
Back
Top