# Howto write SAX2Parser stream in to a file

**URL:** <https://www.ruby-forum.com/t/howto-write-sax2parser-stream-in-to-a-file/83459>\
**Category:** Ruby\
**Created:** [January 26, 2007, 11:29am UTC](https://www.ruby-forum.com/t/howto-write-sax2parser-stream-in-to-a-file/83459 "2007-01-26T11:29:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [January 26, 2007, 11:29am UTC](https://www.ruby-forum.com/t/howto-write-sax2parser-stream-in-to-a-file/83459/1 "2007-01-26T11:29:31Z")

</div>

Hello,

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

… of course doc.parse contain a correct stream :

irb(main):067:0\> doc.inspect  
=\> “#\<REXML::Parsers::SAX2Parser:0x2f4aba8 @procs=[[:characters,  
“title”, #\<Pr  
oc:0x02f3c184@(irb):48\>], [:characters, “link”,  
#Proc:0x02f34114@:51(irb)],  
[:characters, “dc:date”, #Proc:0x02f2b334@:54(irb)], [:characters,  
“dc:subj  
ect”, #Proc:0x02f201a0@:57(irb)]], @tag\_stack=[], @listeners=[],  
@has\_listene  
rs=false, @namespace\_stack=[],  
@parser=#\<REXML::Parsers::BaseParser:0x2f4ab80 @d  
ocument\_status=nil, @source=#\<REXML::IOSource:0x2f4ab08  
@source=#\<File:lapatyXML  
.xml\>, @er\_source=#\<File:lapatyXML.xml\>, @buffer=”\<?xml  
version=\“1.0\” enc  
oding=\“UTF-8\”?\>", @line=0, @line\_break="\>", @orig="\<?xml  
version=\“1  
.0\” encoding=\“UTF-8\”?\>", @to\_utf=false, @encoding=“UTF-8”\>,  
@closed=  
nil, @stack=[], @tags=[], @entities=[]\>, @entities={}\>"  
irb(main):068:0\>

irb(main):018:0\> doc.type  
(irb):18: warning: Object#type is deprecated; use Object#class  
=\> REXML::Parsers::SAX2Parser

## irb(main):019:0\> doc.class =\> REXML::Parsers::SAX2Parser

Any suggestion ?

Thanks in advance  
Lgs

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [January 27, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/howto-write-sax2parser-stream-in-to-a-file/83459/2 "2007-01-27T14:46:41Z")

</div>

Luca g. Soave schrieb:

> file = File.new “lapatyXML.xml”  
> 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [January 30, 2007, 11:30am UTC](https://www.ruby-forum.com/t/howto-write-sax2parser-stream-in-to-a-file/83459/3 "2007-01-30T11:30:44Z")

</div>

I’d like to thank you very much Pit,  
that’s working fine.

Bye Lgs
