目前,我有一个XML文档(名为Food_Display_Table.xml),其数据格式如下:
<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>
我想以人类可读的格式打印一些这些信息。像这样:
-----
Sour cream dip
Calories: 133.65000
Saturated Fats: 7.36898
-----
到目前为止,我已经尝试过这个,但它不起作用:
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
答案 0 :(得分:3)
使用Xpath。我倾向于使用Nokogiri,因为我更喜欢API。
路径是硬编码的:
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
或者有点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
答案 1 :(得分:1)
我会这样做,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
如果我不得不使用REXML(我曾经爱过,但现在更喜欢Nokogiri),以下作品:
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
#=> ---------------