[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby & XML

Jonathan Heinen

5/18/2006 1:02:00 AM

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
2 Answers

Phlip

5/18/2006 2:18:00 AM

0

Jonathan Heinen wrote:

> 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.

--
Phlip
http://c2.com/cgi/wik... <-- NOT a blog!!!


Ross Bamford

5/18/2006 6:51:00 AM

0

On Thu, 18 May 2006 03:18:29 +0100, Phlip <phlipcpp@yahoo.com> wrote:

> Jonathan Heinen wrote:
>
>> 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!
>
> 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.ruby...

--
Ross Bamford - rosco@roscopeco.remove.co.uk