PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Overwrite a File (rexml)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Overwrite a File (rexml)

Réponse
 
LinkBack Outils de la discussion
Vieux 08/06/2008, 10h48   #1
Rob Boellaard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Overwrite a File (rexml)




Hello,

I am trying to get some simple create/update/delete xml-element to
work with REXML, but I run into trouble when writing to file. Here's a
code example:

def save
file = File.open(@@filePath, "r+")
doc = Document.new(file)
root = doc.root


#create/update/delete elements in the root

file.rewind
formatter = REXML::Formatters:efault.new( 5 )
formatter.write( doc, file)
file.close

end

at first I was just adding and updating, and I noticed I needed to put
the line:
file.rewind
or it would just append the root elements to the existing ones in the
file, thereby creating doubles.
Now that I have reached delete, I get the following behaviour:

say the file contains:

element_1
element_2
element_3
element_4

and I want to delete element_2, the result in the file is:

element_1
element_3
element_4
element_4



to me, this:
formatter.write( doc, file)
implies that the entire xml doc would be written to the file, and as a
result the file should only contain the doc (or the root of the doc),
and nothing else.

I am not very experienced in Ruby, and tried to resolve this way:

def save
file = File.open(@@filePath, "r+")
doc = Document.new(file)
root = doc.root
file.close

#create/update/delete elements in the root

file = File.open(@@filePath, "w")
formatter = REXML::Formatters:efault.new( 5 )
formatter.write( doc, file)
file.close

end


this works, but I feel there should be a better approach, because
having to open the same file twice seems awkward .

Thanks in advance,

Rob
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/06/2008, 13h53   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Overwrite a File (rexml)

2008/6/8 Rob Boellaard <rboell@tuparev.com>:
>
>
>
> Hello,
>
> I am trying to get some simple create/update/delete xml-element to
> work with REXML, but I run into trouble when writing to file. Here's a
> code example:
>
> def save
> file = File.open(@@filePath, "r+")
> doc = Document.new(file)
> root = doc.root
>
>
> #create/update/delete elements in the root
>
> file.rewind
> formatter = REXML::Formatters:efault.new( 5 )
> formatter.write( doc, file)
> file.close
>
> end
>
> at first I was just adding and updating, and I noticed I needed to put
> the line:
> file.rewind
> or it would just append the root elements to the existing ones in the
> file, thereby creating doubles.
> Now that I have reached delete, I get the following behaviour:
>
> say the file contains:
>
> element_1
> element_2
> element_3
> element_4
>
> and I want to delete element_2, the result in the file is:
>
> element_1
> element_3
> element_4
> element_4
>
>
>
> to me, this:
> formatter.write( doc, file)
> implies that the entire xml doc would be written to the file, and as a
> result the file should only contain the doc (or the root of the doc),
> and nothing else.
>
> I am not very experienced in Ruby, and tried to resolve this way:
>
> def save
> file = File.open(@@filePath, "r+")
> doc = Document.new(file)
> root = doc.root
> file.close
>
> #create/update/delete elements in the root
>
> file = File.open(@@filePath, "w")
> formatter = REXML::Formatters:efault.new( 5 )
> formatter.write( doc, file)
> file.close
>
> end
>
>
> this works, but I feel there should be a better approach, because
> having to open the same file twice seems awkward .
>
> Thanks in advance,


IMHO you need to seek and truncate the file when opened in rw mode.
Otherwise you do not have control about where the document is written.
Since you are first reading it it is likely that the new content
simply gets appended. HTH

Kind regards

robert


--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 09/06/2008, 14h17   #3
Une Bévue
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Overwrite a File (rexml)

Rob Boellaard <rboell@tuparev.com> wrote:

> I am trying to get some simple create/update/delete xml-element to
> work with REXML, but I run into trouble when writing to file. Here's a
> code example:


instead of using rexml, I'd suggest you 'xml/smart' which as a "modify"
method.

for example I'm using :
XML::Smart.modify( "#{file}" ) do | doc |
node=doc.root.find("//xhtml:p[@id = 'maj']", {"xhtml" =>
"http://www.w3.org/1999/xhtml"}).first
if node
node.text=File.stat("#{file}").mtime.strftime("màj le %d %m %y
%H:%M")
puts "file '#{file}' updated!"
else
puts "No node \"//xhtml:p[@id = 'maj']\" for file '#{file}'!"
end
end

in order to modify the date (writen in <p id='maj'><</p>) from file's
mtime.
--
Une Bévue
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11979 seconds with 11 queries