Ruby & XML

J

Jonathan Heinen

Hi!

Is there any possibility to use XML with ruby like with php?
I will load a file and then access the file like a object and the
content as member variables of the object!

Is this possible?

Jonathan
 
P

Phlip

Jonathan said:
Is there any possibility to use XML with ruby like with php?
I will load a file and then access the file like a object and the content
as member variables of the object!

Totally, but almost.

The two styles of XML parsing are SAX and DOM. The former treats the XML as
a stream and calls your registered when it encounters matching nodes. That
naturally tunes for slipping huge files through narrow memory.

You are asking for DOM style, where you just blast the file into one object
model in memory, and then walk the model looking for elements. I don't know
about SAX style, but REXML supports DOM-style wonderfully. Google for all
that, and XPath - it's absurdly easy.

Except I suspect REXML doesn't bother to do the Magic Member thing. You just
have to say attribute['name'] to get the target. That's a small price for
otherwise incredible convenience.
 
R

Ross Bamford

You are asking for DOM style, where you just blast the file into one
object
model in memory, and then walk the model looking for elements. I don't
know
about SAX style, but REXML supports DOM-style wonderfully. Google for all
that, and XPath - it's absurdly easy.

REXML is very capable, stable, and included with the ruby standard
library, so is ideal for your purposes. However, I would make the point
that it is rather slow - If performance is an issue, or you're doing lots
and lots of XML, you might want to take a look at a native library
binding, e.g.:

http://libxml.rubyforge.org/
 

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

No members online now.

Forum statistics

Threads
474,296
Messages
2,571,535
Members
48,281
Latest member
DaneLxa72

Latest Threads

Top