Forum: Ruby Ruby classes and objects from XML

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0788f26db45fe53107203f227cd75cb6?d=identicon&s=25 Imobach González Sosa (Guest)
on 2007-01-03 10:59
(Received via mailing list)
Hi all,

I have an XML file generated from TaskJuggler[1] and I'd like to have
the objects described in that file (Projects, Resources, Tasks and
Bookings) available as Ruby classes and objects, so I can write
reports about the project.

In summary, I have and XML file (argh!) and I want to convert it
"automagically" into Ruby classes ("struct"-like classes will suffice)
and objects, without having to write a "parser".

Does exist any library/module that could do that job?

Thank you in advance.

[1] http://www.taskjuggler.org/
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-01-03 11:51
(Received via mailing list)
On 03.01.2007 10:57, Imobach González Sosa wrote:
>
> [1] http://www.taskjuggler.org/

These are your options as far as I can see at the moment:

1. use REXML DOM

You can read the file in a single line and get a document object.  You
can then extract attributes via XPath or direct traversal.  In this case
you need zero code for the parsing / reading but the extraction might be
a bit more complex than you want (using XPath expressions is more
complex than just using obj.some_attribute).

2. use REXML stream parser with Hash

You will have to code a generic listener to XML stream parsing events
(not too complicated) that basically transfers data seen into a tree if
Hash instances.  XML element names become Hash keys and nested elements
become Hashes as values.  You probably need additional (fixed) keys for
storing XML attributes and a link to the parent element.

Stream parsing:
http://www.germane-software.com/software/rexml/doc...

3. use REXML stream parser with OpenStruct

Basically the same as option 2 but you use OpenStruct instead of Hash.

4. use REXML stream parser with report on the fly

Depending on your reporting needs you might not have to construct an
object tree at all but can create your report while you go through the
file.  This is the most efficient approach for large files.

5. use REXML stream parser with custom classes on the fly

You implement a listener for XML stream parsing events that will create
custom classes on the fly, i.e. when it sees a "Person" it will create
class Person; then when it sees a nested element it will create a
attribute for it etc.  This is the most complex and think it's probably
not worth the effort.

6. XML Mapping

Not sure whether that fits your needs or is stable at all:
http://raa.ruby-lang.org/project/xml-mapping/

Or this one
http://raa.ruby-lang.org/project/xmltreebuilder/

Other XML related libs:
http://raa.ruby-lang.org/cat.rhtml?category_major=...

7. use an XSLT tool

You could as well use an XSLT tool to create your report as
transformation of your input file.  Whether that works depends on your
reporting requirements.


Depending on what kinds of reports you want to do, option 1 or 2/3 might
be the most efficient.  Especially if you just want to do something like
"task x has n sub tasks" that can be easily accomplished with XPath.

If those XML files are large, then you should go with one of the stream
parsing approaches because they are more memory efficient.  Also you can
filter the data while you go (i.e. ignore attributes and nested elements
you are not interested in) and thus further optimize your app.  The most
efficient is certainly 4 if it's feasible.

Note, there are other XML parsers for Ruby around.  I prefer to start
wiht REXML and only convert to something else if performance is an issue
simply because it's part of the standard distribution and has a nice
interface.

Kind regards

	robert
82e62c756d89bc6fa0a0a2d7f2b1e617?d=identicon&s=25 Ross Bamford (Guest)
on 2007-01-03 12:05
(Received via mailing list)
On Wed, 03 Jan 2007 10:47:46 -0000, Robert Klemme
<shortcutter@googlemail.com> wrote:

>
> [snipped good suggestions]
>
> Note, there are other XML parsers for Ruby around.  I prefer to start
> wiht REXML and only convert to something else if performance is an issue
> simply because it's part of the standard distribution and has a nice
> interface.
>

Just to add, if performance is an issue in this case, or if the OP
simply
fancies a change, theres DigestR which is custom-designed for this sort
of
parsing:

	http://digestr.rubyforge.org/

There's also the older, REXML-based xmldigester, but I don't know it's
current status I'm afraid:

	http://xmldigester.rubyforge.org/

Cheers,
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-01-03 15:22
(Received via mailing list)
On Jan 3, 2007, at 3:57 AM, Imobach González Sosa wrote:

> In summary, I have and XML file (argh!) and I want to convert it
> "automagically" into Ruby classes ("struct"-like classes will suffice)
> and objects, without having to write a "parser".
>
> Does exist any library/module that could do that job?

Have a look at:

http://xml-simple.rubyforge.org/

Hope that helps.

James Edward Gray II
4adc88eff7bf918a302b1f52a4a248b3?d=identicon&s=25 Tom Pollard (tomp)
on 2007-01-03 22:42
(Received via mailing list)
On Jan 3, 2007, at 9:21 AM, James Edward Gray II wrote:

>
> http://xml-simple.rubyforge.org/

XmlSimple looks like a quick way to get a usable data structure from
a piece of XML, but it's just a hash, not objects.

The ROXML project sounds like it comes closest to doing what the OP
was looking for,

http://roxml.rubyforge.org/

Unfortunately, I haven't used it myself yet, so I can't speak to its
robustness.  Does anyone else know anything more about ROXML?  It
strikes me as a great idea.

TomP
This topic is locked and can not be replied to.