Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:58:38+00:00 2026-05-23T18:58:38+00:00

I’m trying to use Ruby’s Nokogiri to parse large (1 GB or more) XML

  • 0

I’m trying to use Ruby’s Nokogiri to parse large (1 GB or more) XML files. I’m testing code on a smaller file, containing only 4 records available here. I’m using Nokogiri version 1.5.0, Ruby 1.8.7 on Ubuntu 10.10. Since I don’t understand SAX very well, I’m trying Nokogiri::XML::Reader to start.

My first attempt, to retrieve the content of the PMID tag, looks like this:

#!/usr/bin/ruby
require "rubygems"
require "nokogiri"

file   = ARGV[0]
reader = Nokogiri::XML::Reader(File.open(file))
p      = []
reader.each do |node|
  if node.name == "PMID"
    p << node.inner_xml
  end
end

puts p.inspect

Here’s what I hoped to see:

["21714156", "21693734", "21692271", "21692260"]

Here’s what I actually saw:

["21714156", "", "21693734", "", "21692271", "", "21692260", ""]

It seems that for some reason, my code is finding, or generating, an extra, empty PMID tag for every instance of PMID. Either that or inner_xml does not work as I thought.

I’d be grateful if anyone could confirm that my code and data generates the result shown and suggest where I’m going wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:58:38+00:00Added an answer on May 23, 2026 at 6:58 pm

    Each element in the stream comes through as two events: one to open the element and one to close it. The opening event will have

    node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
    

    and the closing event will have

    node.node_type == Nokogiri::XML::Reader::TYPE_END_ELEMENT
    

    The empty strings you’re seeing are just the element closing events. Remember that with SAX parsing, you’re basically walking through a tree so you need the second event to tell you when you’re going back up and closing an element.

    You probably want something more like this:

    reader.each do |node|
      if node.name == "PMID" && node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
        p << node.inner_xml
      end
    end
    

    Or perhaps:

    reader.each do |node|
      next if node.name      != 'PMID'
      next if node.node_type != Nokogiri::XML::Reader::TYPE_ELEMENT
      p << node.inner_xml
    end
    

    Or some other variation on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm parsing an XML file, the creators of it stuck in a bunch social
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.