From: James Britt Date: 2007-02-22T10:06:56+09:00 Subject: Re: Rexml - StreamListener - Where I am in the XML? beikel.meikel@web.de wrote: > Hi, > > I'm using REXML::StreamListener to analyze a big xml file. My ruby > code looks like this: > > --- code start here --- > > require 'rexml/document' > require 'rexml/streamlistener' > > class MyListener > include REXML::StreamListener > def tag_start(name, attrs) > # anything to do ... > end > def text(text) > # anything to do ... > end > end > > REXML::Document.parse_stream( File.open( xmlfile), MyListener.new) > > --- code ends here --- > > At the "tag_start" method I need sometimes the information where I am > in the xml. What is my parent tag and so on. Is there is a method to > get this information at this time? In general, the big value of a stream parser is that it is not holding onto much state, so memory needs stay small regardless of the size of the XML. State tracking is left to the application developer. The REXML pull-parser lets you peek at the next event; not sure offhand if it goes the other way. But I suspect that with the stream and pull parsers (one of which sits on the other under the hood, so they are more or less the same), once an event is off the stack, it is gone. Stream parsing works really well when you have a large source of regularly structured data (e.g., XML dump of a database table), such that you can grab and stash in memory just what you need, work with it (perhaps as a transient DOM), then discard it and move on. -- James Britt "Trying to port the desktop metaphor to the Web is like working on how to fuel your car with hay because that is what horses eat." - Dare Obasanjo