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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:31:19+00:00 2026-05-25T17:31:19+00:00

Currently, I have an XML document (called Food_Display_Table.xml) with data in a format like

  • 0

Currently, I have an XML document (called Food_Display_Table.xml) with data in a format like this:

<Food_Display_Table>
    <Food_Display_Row>
        <Food_Code>12350000</Food_Code>
        <Display_Name>Sour cream dip</Display_Name>
        ....
        <Solid_Fats>105.64850</Solid_Fats>
        <Added_Sugars>1.57001</Added_Sugars>
        <Alcohol>.00000</Alcohol>
        <Calories>133.65000</Calories>
        <Saturated_Fats>7.36898</Saturated_Fats>
    </Food_Display_Row>
    ...
</Food_Display_Table>

I would like to print some of this information in human readable format. Like this:

-----
Sour cream dip
Calories: 133.65000
Saturated Fats: 7.36898
-----

So far, I have tried this, but it doesn’t work:

require 'rexml/document'
include REXML

data = Document.new File.new("Food_Display_Table.xml", "r")

data.elements.each("*/*/*") do |foodcode, displayname, portiondefault, portionamount, portiondisplayname, factor, increments, multiplier, grains, wholegrains, orangevegetables, darkgreenvegetables, starchyvegetables, othervegetables, fruits, milk, meats, soy, drybeans, oils, solidfats, addedsugars, alcohol, calories, saturatedfats|
  puts "----"
  puts displayname
  puts "Calories: {calories}"
  puts "Saturated Fats: {saturatedfats}"
  puts "----"
end
  • 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-25T17:31:20+00:00Added an answer on May 25, 2026 at 5:31 pm

    Use Xpath. I tend to go with Nokogiri as I prefer the API.

    With the paths hard-coded:

    doc = Nokogiri::XML(xml_string)
    doc.xpath(".//Food_Display_Row").each do |node|
      puts "-"*5
      puts "Name: #{node.xpath('.//Display_Name').text}"
      puts "Calories: #{node.xpath('.//Calories').text}"
      puts "Saturated Fats: #{node.xpath('.//Saturated_Fats').text}"
      puts "-"*5
    end
    

    or for something a bit DRYer.

    nodes_to_display = ["Display_Name", "Calories", "Saturated_Fats"]
    
    doc = Nokogiri::XML(xml_string)
    doc.xpath(".//Food_Display_Row").each do |node|
      nodes_to_display.each do |node_name|
        if value = node.at_xpath(".//#{node_name}")
          puts "#{node_name}: #{value.text}"
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an XML document like this: <?xml version=1.0 encoding=ISO-8859-1?> <?xml-stylesheet type=text/xsl href=./transform.xsl?>
I have an Document object that is modeling a XML like this one: <RootNode
I have an XML document at a Url http://test.com/data.xml which I am currently polling
I currently have a datasource from a client in the form of XML, this
Currently, I have this version of the autocomplete control working when returning XML from
I currently have this XML schema : <PSC5> <POI_ORI> <CIT>LIM</CIT> </POI_ORI> </PSC5> if user
In my Python app, I have an XML document that I'd like to transform
I have this XML (stored in a C# string called myXML ) <?xml version=1.0
I have XML data that looks like: <xml> The captial of <place pid=1>South Africa</place>
Currently I have a solution that builds an XML document in a number of

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.