# Ruby rexml stream mode

**URL:** <https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534>\
**Category:** Ruby\
**Created:** [June 22, 2010, 10:16am UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534 "2010-06-22T10:16:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 22, 2010, 10:16am UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534/1 "2010-06-22T10:16:36Z")

</div>

hi,

would any one please help me with parse the xml below with STREAM MODE  
of ruby rexml

i would want to get out the following for each event:

1. event id
2. title
3. url
4. description

\<?xml version="1.0" encoding="UTF-8"?\> 138 5 28 1 5 1 5 0.043 Summer Solstice http://eventful.com/tampa/events/summer-solstice-/ E0-001-030989218-5 On the longest day of the year, safely observe the Sun through MOSIÎ“Ã‡Ã–s solar telescopes. See for yourself some of the violent and extremely powerful events that occur on the sun, such as sunspots and solar prominences. All telescope activities are weather dependent. Clear skies are required to use the telescopes during the day and evening. Weather conditions like rain or clouds will force the cancellation of SkyWatch events. 2010-06-21 00:00:00 2010-06-22 00:00:00 V0-001-000153419-6 http://eventful.com/tampa/venues/mosi-tampa-museum- of- science-and-industry-/V0-001-000153419-6 Mosi Tampa Museum of Science and Industry 1 4801 E Fowler Ave Tampa Florida FL 33617 United States US USA 28.053545 -82.404529 EVDB Geocoder 1 0 0 1 -1 -1 2010-05-28 11:49:06 mositampa 2010-05-28 13:07:10 http://static.eventful.com/images/small/ I0-001/003/067/081-4.jpeg 48 48 http://static.eventful.com/images/thumb/ I0-001/003/067/081-4.jpeg 48 48 http://static.eventful.com/images/small/ I0-001/003/067/081-4.jpeg 48 48 http://static.eventful.com/images/medium/ I0-001/003/067/081-4.jpeg 128 128 1

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 22, 2010, 11:31am UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534/2 "2010-06-22T11:31:22Z")

</div>

kevid wrote:

> would any one please help me with parse the xml below with STREAM MODE  
> of ruby rexml

Sure: show us what code you’ve written so far, and what exact error you  
get or where you’re stuck.

The starting point, of course, is to type “ruby rexml stream parsing”  
into google, where the first hit is  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

Scroll down to this section:

* * *

Stream Parsing

REXML stream parsing requires you to supply a Listener class. When REXML  
encounters events in a document (tag start, text, etc.) it notifies your  
listener class of the event. You can supply any subset of the methods,  
but make sure you implement method\_missing if you don’t implement them  
all. A StreamListener module has been supplied as a template for you to  
use.

Stream parsing  
list = MyListener.new  
source = File.new “mydoc.xml”  
REXML::Document.parse\_stream(source, list)

Stream parsing in REXML is much like SAX, where events are generated  
when the parser encounters them in the process of parsing the document.  
When a tag is encountered, the stream listener’s tag\_start() method is  
called. When the tag end is encountered, tag\_end() is called. When text  
is encountered, text() is called, and so on, until the end of the stream  
is reached. One other note: the method entity() is called when an  
&entity; is encountered in text, and only then.

## Please look at the StreamListener API for more information.[1]

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 22, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534/3 "2010-06-22T16:10:46Z")

</div>

tanks Brain,

I have actually gone through the link you gave me. I am new to the  
concept of rexml stream mode xml parsing.

An example with the above xml doc. will get me started.

I anticipate your response or that of any other person

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 22, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534/4 "2010-06-22T17:43:28Z")

</div>

kevid wrote:

> I have actually gone through the link you gave me. I am new to the  
> concept of rexml stream mode xml parsing.
> 
> An example with the above xml doc. will get me started.
> 
> I anticipate your response or that of any other person

Which part of the text I quoted do you not understand?

“REXML stream parsing requires you to supply a Listener class”

Do you know how to create a class in Ruby? It’s just:

class MyListener  
end

“When a tag is encountered, the stream listener’s tag\_start() method is  
called. When the tag end is encountered, tag\_end() is called. When text  
is encountered, text() is called, and so on, until the end of the stream  
is reached”

So, add those methods to your class, just like it says.

class MyListener  
def tag\_start(\*args)  
puts “Got tag\_start with #{args.inspect}”  
end  
def tag\_end(\*args)  
puts “Got tag\_start with #{args.inspect}”  
end  
def text(\*args)  
puts “Got text with #{args.inspect}”  
end  
end

The rest of the code you need is directly given in the example.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 22, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/ruby-rexml-stream-mode/190534/5 "2010-06-22T17:47:44Z")

</div>

Brian C. wrote:

> The rest of the code you need is directly given in the example.

“A StreamListener module has been supplied as a template for you to  
use.”

That means you can write:

class MyListener  
include REXML::StreamListener  
end

and have empty (do-nothing) implementations of tag\_start, tag\_end etc,  
which you can selectively override for the things you’re interested in.  
If you follow the link to

[http://www.germane-software.com/software/rexml/doc/classes/REXML/StreamListener.html](http://www.germane-software.com/software/rexml/doc/classes/REXML/StreamListener.html)

and open each method up, you’ll see the correct template for the method.  
e.g.

class MyListener  
include REXML::StreamListener  
def tag\_start(name,attrs)  
puts “Yo! Got start tag #{name.inspect} with attrs #{attrs.inspect}”  
end  
end
