Luca g. Soave
1/30/2007 10:29:00 AM
I'd like to thank you very much Pit,
that's working fine.
Bye Lgs
On 27 Gen, 14:45, Pit Capitain <p...@capitain.de> wrote:
> Luca g. Soave schrieb:
>
>
>
> > I'm new to Ruby and trying to write a stream in to a file,
> > but I can just get an empty one. I tryed in different way
> > found heare on the forum ( IO lib, Rio lib,
> > out_file << "#{doc.parse}" and so on ).
>
> > Here I show you the simplest :
>
> > -----------------------------------------
> > require 'rexml/parsers/sax2parser'
>
> > file = File.new "lapatyXML.xml"
> > doc = REXML::Parsers::SAX2Parser.new file
>
> > doc.listen(:characters, ["title"]) do |title|
> > print "\"#{title}\" : "
> > end
> > doc.listen(:characters, ["link"]) do |link|
> > print "#{link} : "
> > end
> > doc.listen(:characters, ["dc:date"]) do |date|
> > print "#{date} : "
> > end
> > doc.listen(:characters, ["dc:subject"]) do |subject|
> > puts "#{subject}"
> > end
>
> > File.open("outfile.xls","w") do |out_file|
> > out_file.write "#{doc.parse}"
> > end
> > ------------------------------------------
>
> > Nothing seems to work, I just get an empty file "outfile.xls" .Luca, welcome to Ruby! I've never used REXML before, so all this might
> be completely wrong, but with the line
>
> out_file.write "#{doc.parse}"
>
> you are writing the result of the #parse method to the output file,
> which might be just an empty string if the result is nil. I think all
> the calls of print and puts in your listeners just write to the console
> where you run your script. To get what you want I would try to change
> your program to:
>
> require 'rexml/parsers/sax2parser'
>
> file = File.new "lapatyXML.xml"
> doc = REXML::Parsers::SAX2Parser.new file
>
> File.open("outfile.xls","w") do |out_file|
>
> doc.listen(:characters, ["title"]) do |title|
> out_file.print "\"#{title}\" : "
> end
> doc.listen(:characters, ["link"]) do |link|
> out_file.print "#{link} : "
> end
> doc.listen(:characters, ["dc:date"]) do |date|
> out_file.print "#{date} : "
> end
> doc.listen(:characters, ["dc:subject"]) do |subject|
> out_file.puts "#{subject}"
> end
>
> doc.parse
>
> end
>
> Regards,
> Pit