首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用REXML读取、编辑和保存现有的XML文件

使用REXML读取、编辑和保存现有的XML文件
EN

Stack Overflow用户
提问于 2014-03-17 18:03:57
回答 1查看 3K关注 0票数 0

我是Ruby的初学者,我编写了一些代码来学习如何用REXML读取/更新Ruby中的XML:

这是我的密码:

代码语言:javascript
运行
复制
# 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部分,但我不知道如何解决这个问题!

我在网上搜索,却找不到办法让这件事行得通。我需要你们的帮助伙计们!

EN

回答 1

Stack Overflow用户

发布于 2014-03-17 19:21:08

替换

代码语言:javascript
运行
复制
$doc.write("som.xml", 2)

使用

代码语言:javascript
运行
复制
$doc.write(File.open("som.xml","w"), 2) ## Must open File in "w" mode in order to write

只有当StatesOfMind 标记存在于som.xml中时,以下代码才能工作

代码语言:javascript
运行
复制
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”(注意复数)当前的

代码语言:javascript
运行
复制
<StatesOfMind>
<StatesOfMind>

,这是确切的代码

代码语言:javascript
运行
复制
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)

代码语言:javascript
运行
复制
<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)

代码语言:javascript
运行
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22461942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档