开发者

How would I parse this XML with Ruby?

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 displayna开发者_Go百科me
  puts "Calories: {calories}"
  puts "Saturated Fats: {saturatedfats}"
  puts "----"
end


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


I'd do it like this, with Nokogiri:

require 'nokogiri' # gem install nokogiri
doc = Nokogiri::XML(IO.read('Food_Display_Table.xml'))

good_fields = %w[ Calories Saturated_Fats ]

puts "-"*5
doc.search("Food_Display_Row").each do |node|
  puts node.at('Display_Name').text
  node.search(*good_fields).each do |node|
    puts "#{node.name.gsub('_',' ')}: #{node.text}" 
  end
  puts "-"*5
end

If I had to use REXML (which I used to love, but now love Nokogiri more), the following works:

require 'rexml/document'
doc = REXML::Document.new( IO.read('Food_Display_Table.xml') )

separator = "-"*15
puts separator
desired = %w[ Calories Saturated_Fats ]
doc.root.elements.each do |row|
  puts REXML::XPath.first( row, 'Display_Name' ).text
  desired.each do |node_name|
    REXML::XPath.each( row, node_name ) do |node|
      puts "#{node_name.gsub('_',' ')}: #{node.text}"
    end
  end
  puts separator
end

#=> ---------------
#=> Sour cream dip
#=> Calories: 133.65000
#=> Saturated Fats: 7.36898
#=> ---------------
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜