简体   繁体   中英

Read, edit, and save existing XML file with REXML

Im' a beginner with Ruby and I made a little code to learn how to read/update XML in Ruby with REXML :

Here's my code :

# 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

Everything works well, except for the fact that the data I add aren't stored in the XML fact. Obviously, the problem comes from the $doc.write part, but I cannot figure how to fix this !

I searched on the internet, and cannot finds a way to make this works. I need your helps, guys !

Replace

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

With

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

The below code would only work if StatesOfMind tag exists in 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

NOTE:

$doc = Document.new File.new("som.xml") means that som.xml already exists else it will throw an error. As per OP comment som.xml isn't empty so I have updated the code accordingly.


Input XML(som.xml): som.xml must have at the minimum root element "StatesOfMind"(Notice Plural) present

<StatesOfMind>
<StatesOfMind>

This is the exact Code

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

Output XML(som.xml) generated on the first run

<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>

Output XML(som.xml) generated on the second run

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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