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:
- event id
- title
- url
- description
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:
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
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.
HTH,
Brian.
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
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs