ruby rexml stream mode

K

kevid

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"?>

<search>
<total_items>138</total_items>
<page_size>5</page_size>
<page_count>28</page_count>
<page_number>1</page_number>
<page_items>5</page_items>
<first_item>1</first_item>
<last_item>5</last_item>
<search_time>0.043</search_time>
<events>
<event id="E0-001-030989218-5">
<title>Summer Solstice</title>
<url>http://eventful.com/tampa/events/summer-solstice-/
E0-001-030989218-5</url>
<description>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.</description>
<start_time>2010-06-21 00:00:00</start_time>
<stop_time>2010-06-22 00:00:00</stop_time>
<tz_id></tz_id>
<tz_olson_path></tz_olson_path>
<tz_country></tz_country>
<tz_city></tz_city>
<venue_id>V0-001-000153419-6</venue_id>
<venue_url>http://eventful.com/tampa/venues/mosi-tampa-museum-
of-
science-and-industry-/V0-001-000153419-6</venue_url>
<venue_name>Mosi Tampa Museum of Science and Industry</
venue_name>
<venue_display>1</venue_display>
<venue_address>4801 E Fowler Ave</venue_address>
<city_name>Tampa</city_name>
<region_name>Florida</region_name>
<region_abbr>FL</region_abbr>
<postal_code>33617</postal_code>
<country_name>United States</country_name>
<country_abbr2>US</country_abbr2>
<country_abbr>USA</country_abbr>
<latitude>28.053545</latitude>
<longitude>-82.404529</longitude>
<geocode_type>EVDB Geocoder</geocode_type>
<all_day>1</all_day>
<recur_string></recur_string>
<trackback_count>0</trackback_count>
<calendar_count>0</calendar_count>
<comment_count></comment_count>
<link_count>1</link_count>
<going_count>-1</going_count>
<watching_count>-1</watching_count>
<created>2010-05-28 11:49:06</created>
<owner>mositampa</owner>
<modified>2010-05-28 13:07:10</modified>
<performers></performers>
<image>
<url>http://static.eventful.com/images/small/
I0-001/003/067/081-4.jpeg</url>
<width>48</width>
<height>48</height>
<caption></caption>
<thumb>
<url>http://static.eventful.com/images/thumb/
I0-001/003/067/081-4.jpeg</url>
<width>48</width>
<height>48</height>
</thumb>
<small>
<url>http://static.eventful.com/images/small/
I0-001/003/067/081-4.jpeg</url>
<width>48</width>
<height>48</height>
</small>
<medium>
<url>http://static.eventful.com/images/medium/
I0-001/003/067/081-4.jpeg</url>
<width>128</width>
<height>128</height>
</medium>
</image>
<privacy>1</privacy>
<calendars></calendars>
<groups></groups>
<going>
<user>
<username></username>
</user>
</going>
</event>
</events>
</search>
 
B

Brian Candler

kevid said:
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

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]
 
K

kevid

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
 
B

Brian Candler

kevid said:
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.
 
B

Brian Candler

Brian said:
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

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
 

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top