繁体   English   中英

使用REXML读取,编辑和保存现有XML文件

[英]Read, edit, and save existing XML file with REXML

我是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部分,但我无法弄清楚如何解决这个问题!

我在互联网上搜索,找不到让这种方法有效的方法。 伙计们,我需要你的帮助!

更换

$doc.write("som.xml", 2)

$doc.write(File.open("som.xml","w"), 2) ## Must open File in "w" mode in order to write

以下代码仅在StatesOfMind存在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM