我是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部分,但我不知道如何解决这个问题!
我在网上搜索,却找不到办法让这件事行得通。我需要你们的帮助伙计们!
发布于 2014-03-17 11:21:08
替换
$doc.write("som.xml", 2)
使用
$doc.write(File.open("som.xml","w"), 2) ## Must open File in "w" mode in order to write
只有当StatesOfMind
标记存在于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
注:
$doc = Document.new File.new("som.xml")
意味着som.xml
已经存在,否则会引发错误。根据OP注释,som.xml
不是空的,因此我已经相应地更新了代码。
输入XML( som.xml ):som.xml必须至少具有根元素“StatesOfMind”(注意复数)当前的
<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>
https://stackoverflow.com/questions/22461942
复制相似问题