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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:31:26+00:00 2026-05-20T11:31:26+00:00

i try to extract data from a xml in rails application with the Nokogiri

  • 0

i try to extract data from a xml in rails application with the Nokogiri gem,

the xml :

<item>
    <description>
        <![CDATA[<img src="something" title="anothething">
        <p>text, bla bla...</p>]]>
    </description>
</item>

Actually i do something like this to extract data from the xml :

def test_content
    @return = Array.new
    site = 'http://www.les-encens.com/modules/feeder/rss.php?id_category=0'
    @doc = Nokogiri::XML(open(site, "UserAgent" => "Ruby-OpenURI"))
    @doc.xpath("//item").each do |n|
        @return << [
            n.xpath('description')
        ] 
   end
end

Could you show me how extract just the src attribute from the img tag ?

Edit :
I have replace the xml by the correct one.

  • 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-20T11:31:26+00:00Added an answer on May 20, 2026 at 11:31 am

    The result of an xpath call made in Nokogiri is going to be a NodeSet, which is simply a list of Nokigiri Nodes

    With this in mind we can just pull examples from the Nokogiri Documentation and adapt them.

    To answer your question, “Could you show me how extract just the src attribute from the img tag ?”, here is one such way.

    #the 'open' method here is part of the open-uri library
    xml = Nokogiri::XML(open(your_url_here))
    
    all_images = xml.xpath("//img") #returns NodeSet (list of Nokogiri Nodes)
    
    image_sources = []
    
    #iterate through each node
    all_images.each() do |node|
      image_sources << node.get_attribute('src') #One method
      #image_sources << node['src'] #Another convention we could use
    end
    

    As Phrogz notes below, a more idomatic way of pulling the ‘src’ attribute from all of the images nodes is to map the ‘src’ attribute directly rather than iterating and pushing onto an array.

    image_sources = all_images.map{ |node| node['src'] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to extract specific data from an xml feed that comes from youtube.
I need to extract data from a .dbf file and transform it into xml.
I'm writing a set of tool in python to extract data from some xml
I'm trying to programmatically extract some data from this url: http://www.treasurydirect.gov/NP/BPDLogin?application=np The issue is
I'm try to extract info from a MySQL DB into a MS SQL DB.
I'm trying to extract some data from various HTML pages using a python program.
I'm trying to extract the StateLongName and StateShortName values from the xml below. I
i need to extract data from this array of objects { data: [ {
I am trying to extract some data from a private forum. I created a
I’m trying to extract the name and satelite coordinations from a XML file and

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.