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 7729085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:52:47+00:00 2026-06-01T05:52:47+00:00

I want to sax-parse in nokogiri, but when it comes to parse xml element

  • 0

I want to sax-parse in nokogiri, but when it comes to parse xml element that have a long and crazy xml element name or a attribute on it.. then everthing goes crazy.

Fore instans if I like to parse this xml file and grab all the title element, how do I do that with nokogiri-sax.

<titles>
    <title xml:lang="sv">Arkivvetenskap</title>
    <title xml:lang="en">Archival science</title>
</titles>
  • 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-06-01T05:52:48+00:00Added an answer on June 1, 2026 at 5:52 am

    In your example, title is the name of the element. xml:lang="sv" is an attribute.
    This parser assumes there are no elements nested inside of title elements

    require 'rubygems'
    require 'nokogiri'
    
    class MyDocument < Nokogiri::XML::SAX::Document
      def start_element(name, attrs)
        @attrs = attrs
        @content = ''
      end
      def end_element(name)
        if name == 'title'
          puts Hash[@attrs]['xml:lang']
          puts @content.inspect
          @content = nil
        end
      end
      def characters(string)
        @content << string if @content
      end
      def cdata_block(string)
        characters(string)
      end
    end
    
    parser = Nokogiri::XML::SAX::Parser.new(MyDocument.new)
    parser.parse(DATA)
    
    __END__
    <titles>
        <title xml:lang="sv">Arkivvetenskap</title>
        <title xml:lang="en">Archival science</title>
    </titles>
    

    This prints

    sv
    "Arkivvetenskap"
    en
    "Archival science"
    

    SAX parsing is usually way too complex. Because of that, I recommend Nokogiri’s standard in-memory parser, or if you really need speed and memory efficiency, Nokogiri’s Reader parser.

    For comparison, here is a standard Nokogiri parser for the same document

    require 'rubygems'
    require 'nokogiri'
    
    doc = Nokogiri::XML(DATA)
    doc.css('title').each do |title|
      puts title['lang']
      puts title.text.to_s.inspect
    end
    
    __END__
    <titles>
        <title xml:lang="sv">Arkivvetenskap</title>
        <title xml:lang="en">Archival science</title>
    </titles>
    

    And here is a reader parser for the same document

    require 'rubygems'
    require 'nokogiri'
    
    reader = Nokogiri::XML::Reader(DATA)
    while reader.read
      if reader.name == 'title' && reader.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
        puts reader.attribute('xml:lang')
        puts reader.inner_xml.inspect # TODO xml decode this, if necessary.
      end
    end
    
    __END__
    <titles>
        <title xml:lang="sv">Arkivvetenskap</title>
        <title xml:lang="en">Archival science</title>
    </titles>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I have a simple but huge xml file like below. I want to parse
I am using SAX to parse XML files. Let's suppose that I want my
I have a problem with a SAX xml parser. I want to parse a
I want to have my application read a document using xml.sax.parse. Things work fine
I have an XML stream I want to parse using SAX. What I actually
I parse a big xml document with Sax, I want to stop parsing the
I want to parse XML in java. It will be DOM or SAX. Read
i want parse xml file, which does't have xml extension, like this: http://bizonek.wrzuta.pl/xml/plik/1ANdXCgTOit/unknow/undefined/643/ my
I have 200,000 XML files I want to parse and store in a database.

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.