我是Ruby的初学者,我制作了一些代码来学习如何使用REXML在Ruby中读取/更新XML:
这是我的代码:
# encoding: utf-8
# Programme used to store information about the state of mind of the user at different times.
# The user can later review all of his "state of mind" with the date he wrote it
require "rexml/document"
include REXML
$doc = Document.new File.new("som.xml")
def reads_old_som
$doc.elements.each("StatesOfMind/StateOfMind") do |e|
puts e.elements["Date"].text
puts e.elements["Content"].text
puts ""
end
end
puts "Describe your state of mind now ! (This must be between 10 and 25 words)"
content = gets.chomp
date_time = Time.now.strftime("%d/%m/%Y %H:%M")
until content.split.length.between?(10,25)
puts "It must be between 10 & 25 words, dude !"
puts "Rewrite me this !"
content = gets.chomp
date_time = Time.now.strftime("%d/%m/%Y %H:%M")
end
som = Element.new("StateOfMind")
som.add_element "Date"
som.elements["Date"].text = date_time
som.add_element "Content"
som.elements["Content"].text = content
$doc.root.add_element som
$doc.write("som.xml", 2)
puts "Ok great ! Your state of mind was saved. Would you like to read the state of mind you wrote in the past ? (Y/N)"
choice = gets.chomp.capitalize
until choice == "Y" || choice == "N"
puts "Please answer with Y or N"
choice = gets.chomp.capitalize
end
if choice == "Y"
puts "Here's all the state of mind you recorded :"
reads_old_som
end
除了我添加的数据没有存储在XML事实中之外,一切都运行良好。显然,问题来自$ doc.write部分,但我无法弄清楚如何解决这个问题!
我在互联网上搜索过,找不到让这种方法有效的方法。伙计们,我需要你的帮助!
答案 0 :(得分:1)
替换
$doc.write("som.xml", 2)
使用
$doc.write(File.open("som.xml","w"), 2) ## Must open File in "w" mode in order to write
以下代码仅在som.xml中存在StatesOfMind
标记的情况下才有效
def reads_old_som
$doc.elements.each("StatesOfMind/StateOfMind") do |e|
puts e.elements["Date"].text
puts e.elements["Content"].text
puts ""
end
end
注意:强>
$doc = Document.new File.new("som.xml")
表示som.xml
已经存在,否则会引发错误。
根据OP注释som.xml
不为空,所以我相应地更新了代码。
输入XML(som.xml):som.xml必须具有最小根元素“StatesOfMind”(Notice Plural)存在
<StatesOfMind>
<StatesOfMind>
这是确切的代码
require "rexml/document"
include REXML
$doc = Document.new File.new("som.xml")
def reads_old_som
$doc.elements.each("StatesOfMind/StateOfMind") do |e|
puts e.elements["Date"].text
puts e.elements["Content"].text
puts ""
end
end
puts "Describe your state of mind now ! (This must be between 10 and 25 words)"
content = gets.chomp
date_time = Time.now.strftime("%d/%m/%Y %H:%M")
until content.split.length.between?(10,25)
puts "It must be between 10 & 25 words, dude !"
puts "Rewrite me this !"
content = gets.chomp
date_time = Time.now.strftime("%d/%m/%Y %H:%M")
end
som = Element.new("StateOfMind")
som.add_element "Date"
som.elements["Date"].text = date_time
som.add_element "Content"
som.elements["Content"].text = content
$doc.root.add_element som
$doc.write(File.open("som.xml","w"), 2)
puts "Ok great ! Your state of mind was saved. Would you like to read the state of mind you wrote in the past ? (Y/N)"
choice = gets.chomp.capitalize
until choice == "Y" || choice == "N"
puts "Please answer with Y or N"
choice = gets.chomp.capitalize
end
if choice == "Y"
puts "Here's all the state of mind you recorded :"
reads_old_som
end
首次运行时生成的输出XML(som.xml)
<StatesOfMind>
<StateOfMind>
<Date>
17/03/2014 16:30
</Date>
<Content>
Hi This is Kirti How are you doing Nice to meet you.
</Content>
</StateOfMind>
<StatesOfMind>
在第二次运行时生成的输出XML(som.xml)
<StatesOfMind>
<StateOfMind>
<Date>
17/03/2014 16:30
</Date>
<Content>
Hi This is Kirti How are you doing Nice to meet you.
</Content>
</StateOfMind>
<StateOfMind>
<Date>
17/03/2014 16:32
</Date>
<Content>
Hi This is John Doe Nice to meet you. I am doing great.
</Content>
</StateOfMind>
<StatesOfMind>