# Memory considerations when parsing XML file

**URL:** <https://www.ruby-forum.com/t/memory-considerations-when-parsing-xml-file/127735>\
**Category:** Ruby\
**Created:** [January 31, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/memory-considerations-when-parsing-xml-file/127735 "2008-01-31T20:40:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [January 31, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/memory-considerations-when-parsing-xml-file/127735/1 "2008-01-31T20:40:45Z")

</div>

Hi,

I need to quickly parse a large XML file. I didn’t want to use DOM  
parsing since this way you end up with the whole file in the memory. I  
looked at writing my own SAX-style parser and came across this post:  
[http://www.janvereecken.com/2007/4/11/event-driven-xml-parser-in-ruby](http://www.janvereecken.com/2007/4/11/event-driven-xml-parser-in-ruby).

My question is about reading the file from the hard drive into the  
parse\_stream method:  
REXML::Document.parse\_stream(File.open(filename).read, MyListener.new)  
Won’t File.open…read read the whole file in the memory? If yes, then  
nothing was gained. I might just as well read and parse the document  
using DOM since it is easier.

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [January 31, 2008, 9:00pm UTC](https://www.ruby-forum.com/t/memory-considerations-when-parsing-xml-file/127735/2 "2008-01-31T21:00:50Z")

</div>

I’m sorry for rushing with the post. I just read that parse\_stream  
also takes an IO object, so I don’t have to do read on the filename.

Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [January 31, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/memory-considerations-when-parsing-xml-file/127735/3 "2008-01-31T23:26:00Z")

</div>

On 31.01.2008 20:56, Mr\_Tibs wrote:

> I’m sorry for rushing with the post. I just read that parse\_stream  
> also takes an IO object, so I don’t have to do read on the filename.

🙂

And the idiom should rather read

File.open(filename, ‘rb’) do |io|  
REXML::Document.parse\_stream(io, MyListener.new)  
end

i.e. use the block form of File.open.

robert
