SAX-based XPath

G

Gary Shea

I'm posting this in hope of getting some API suggestions.

I'm building a native stream-based Ruby XPath processor (or whatever it
would be called) in order to parse some gigabyte-scale XML files at
work. It will accept multiple XPath expressions and output events (SAX
for now) matching the union of the XPath expressions.

It currently only works with absolute, non-wildcarded, predicate-less
default-axis XPath expressions:

filter = XmlFilter::XPathFilter.new
filter.listener = XmlFilter::RecordingListener.new
filter.xpath = '/a/b/c'

parser = REXML::parsers::SAX2Parser.new(File.open('some_file_path.xml'))
parser.listen = filter
parser.parse

This interface needs to be extended a little to work with multiple XPath
expressions, maybe:

filter.xpath = ['/a/b/c', '/d/e/f']

Any suggestions for a more Ruby-esque way to do it?

Gary
 
R

Robert Klemme

Gary said:
I'm posting this in hope of getting some API suggestions.

I'm building a native stream-based Ruby XPath processor (or whatever
it would be called) in order to parse some gigabyte-scale XML files at
work. It will accept multiple XPath expressions and output events
(SAX for now) matching the union of the XPath expressions.

It currently only works with absolute, non-wildcarded, predicate-less
default-axis XPath expressions:

filter = XmlFilter::XPathFilter.new
filter.listener = XmlFilter::RecordingListener.new
filter.xpath = '/a/b/c'

parser =
REXML::parsers::SAX2Parser.new(File.open('some_file_path.xml'))
parser.listen = filter
parser.parse

This interface needs to be extended a little to work with multiple
XPath expressions, maybe:

filter.xpath = ['/a/b/c', '/d/e/f']

Any suggestions for a more Ruby-esque way to do it?

It seems you could simplify the interface a bit (or add a method) along
the lines of REXML so you can do

File.open('some_file_path.xml') do |io|
XmlFilter::XPathFilter.parse(io, '/a/b/c', '/d/e/f') do |event, filter|
# process event
end
end

I'm unsure about the "filter" block parameter but it might be useful to
know the matching filter criterium. What do you think?

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top