PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > XML to Ruby?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
XML to Ruby?

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 12h39   #1
Jari Williamsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut XML to Ruby?

Is there any Ruby package that will read a XML file and build Ruby
classes for the tags found (classes that can handle reading, writing,
setting attributes, etc), using the object relationships from the XML?
Most preferably, get everything from the DTD and build the Ruby classes
from there?


Best regards,

Jari Williamsson

  Réponse avec citation
Vieux 22/11/2007, 13h13   #2
PWR
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

On 22 Nov, 12:39, Jari Williamsson
<jari.williams...@mailbox.swipnet.se> wrote:
> Is there any Ruby package that will read a XML file and build Ruby
> classes for the tags found (classes that can handle reading, writing,
> setting attributes, etc), using the object relationships from the XML?
> Most preferably, get everything from the DTD and build the Ruby classes
> from there?
>
> Best regards,
>
> Jari Williamsson


I've used REXML which worked very well. The only drawback is that it
is not a validating parser.

Peter.
  Réponse avec citation
Vieux 22/11/2007, 13h39   #3
Rolando Abarca
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

On Nov 22, 2007, at 9:39 AM, Jari Williamsson wrote:

> Is there any Ruby package that will read a XML file and build Ruby
> classes for the tags found (classes that can handle reading,
> writing, setting attributes, etc), using the object relationships
> from the XML? Most preferably, get everything from the DTD and build
> the Ruby classes from there?


some time ago, I built a small class to do that, you can check it here:

http://rolando.cl/xmlable/src/xmlable.rb
http://rolando.cl/xmlable/src/test.rb
http://rolando.cl/xmlable/doc/index.html

> Best regards,
>
> Jari Williamsson



Regards,
--
Rolando Abarca
Phone: +56-9 97851962



  Réponse avec citation
Vieux 22/11/2007, 14h06   #4
Jari Williamsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

Rolando Abarca wrote:

> some time ago, I built a small class to do that, you can check it here:
>
> http://rolando.cl/xmlable/src/xmlable.rb
> http://rolando.cl/xmlable/src/test.rb
> http://rolando.cl/xmlable/doc/index.html


Thanks, but this seems to only make XML data accessible to an existing
class?
It seems like I need to specify a bit better what I need: I'm looking
for a utility that will _generate_ Ruby code (classes) from a
hierarchical XML file.

Let's say this is the XML data:
mymodule.xml
======
<root>
<sub id="1">Some data</sub>
<sub id="2>Some more data</sub>
</root>

The utility would then generate 2 .rb module files, such as:

root.rb:
======
require 'mymodule/sub'
module MyModule
class Root < SomeBaseClass
attr_accessor :sub_elements # Array
# Some more stuff
end

sub.rb:
======
module MyModule
class Sub < SomeBaseClass
attr_accessor :id
# Some more stuff
end
end

The more I think about it, this kind of utility probably is required to
analyze the DTD rather than the XML to get the proper relationships. Any
such utility available?


Best regards,

Jari Williamsson

  Réponse avec citation
Vieux 22/11/2007, 14h08   #5
Jari Williamsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

PWR wrote:
> On 22 Nov, 12:39, Jari Williamsson
> <jari.williams...@mailbox.swipnet.se> wrote:
>> Is there any Ruby package that will read a XML file and build Ruby
>> classes for the tags found (classes that can handle reading, writing,
>> setting attributes, etc), using the object relationships from the XML?
>> Most preferably, get everything from the DTD and build the Ruby classes
>> from there?
>>
>> Best regards,
>>
>> Jari Williamsson

>
> I've used REXML which worked very well. The only drawback is that it
> is not a validating parser.


How do you get REXML to generate Ruby classes?


Best regards,

Jari Williamsson

  Réponse avec citation
Vieux 22/11/2007, 15h05   #6
Marcin Raczkowski
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

Jari Williamsson wrote:
> PWR wrote:
>> On 22 Nov, 12:39, Jari Williamsson
>> <jari.williams...@mailbox.swipnet.se> wrote:
>>> Is there any Ruby package that will read a XML file and build Ruby
>>> classes for the tags found (classes that can handle reading, writing,
>>> setting attributes, etc), using the object relationships from the XML?
>>> Most preferably, get everything from the DTD and build the Ruby classes
>>> from there?
>>>
>>> Best regards,
>>>
>>> Jari Williamsson

>>
>> I've used REXML which worked very well. The only drawback is that it
>> is not a validating parser.

>
> How do you get REXML to generate Ruby classes?
>
>
> Best regards,
>
> Jari Williamsson
>
>

try xml-mapper it builds classes from XML files

  Réponse avec citation
Vieux 22/11/2007, 16h50   #7
Rolando Abarca
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

On Nov 22, 2007, at 11:06 AM, Jari Williamsson wrote:

> Rolando Abarca wrote:
>
>> some time ago, I built a small class to do that, you can check it
>> here:
>> http://rolando.cl/xmlable/src/xmlable.rb
>> http://rolando.cl/xmlable/src/test.rb
>> http://rolando.cl/xmlable/doc/index.html

>
> Thanks, but this seems to only make XML data accessible to an
> existing class?
> It seems like I need to specify a bit better what I need: I'm
> looking for a utility that will _generate_ Ruby code (classes) from
> a hierarchical XML file.


well, that's exactly what my class do: convert from xml to ruby
classes, convert from ruby classes to xml. Check the test file for
examples.

> --


Rolando Abarca
Phone: +56-9 97851962



  Réponse avec citation
Vieux 23/11/2007, 03h31   #8
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

On Nov 22, 7:06 am, Jari Williamsson
<jari.williams...@mailbox.swipnet.se> wrote:
> Rolando Abarca wrote:
> It seems like I need to specify a bit better what I need: I'm looking
> for a utility that will _generate_ Ruby code (classes) from a
> hierarchical XML file.
>
> Let's say this is the XML data:
> mymodule.xml
> ======
> <root>
> <sub id="1">Some data</sub>
> <sub id="2>Some more data</sub>
> </root>
>
> The utility would then generate 2 .rb module files, such as:
>
> root.rb:
> ======
> require 'mymodule/sub'
> module MyModule
> class Root < SomeBaseClass
> attr_accessor :sub_elements # Array
> # Some more stuff
> end
>
> sub.rb:
> ======
> module MyModule
> class Sub < SomeBaseClass
> attr_accessor :id
> # Some more stuff
> end
> end
>
> The more I think about it, this kind of utility probably is required to
> analyze the DTD rather than the XML to get the proper relationships. Any
> such utility available?


I really have no idea how your XML file above maps to the output you
describe. It appears that you just look at the unique element names
and create classes from them. You ignore the hierarchy of the XML
file, you ignore all the attributes of the elements, and you ignore
all the text content of the elements?

So, you just need something like this:

raw_xml = IO.read( 'myfile.xml' )
all_tag_names = raw_xml.scan( /<(\w+)/ ).flatten.uniq
all_tag_names.each{ |tag_name|
# use ERB here to create new files using
# tag_name.capitalize as the foundation for the class name
}
  Réponse avec citation
Vieux 23/11/2007, 13h43   #9
Bob Hutchison
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

Hi,

On 22-Nov-07, at 10:34 PM, Phrogz wrote:

> On Nov 22, 7:06 am, Jari Williamsson
> <jari.williams...@mailbox.swipnet.se> wrote:
>> Rolando Abarca wrote:
>> It seems like I need to specify a bit better what I need: I'm looking
>> for a utility that will _generate_ Ruby code (classes) from a
>> hierarchical XML file.
>>
>> Let's say this is the XML data:
>> mymodule.xml
>> ======
>> <root>
>> <sub id="1">Some data</sub>
>> <sub id="2>Some more data</sub>
>> </root>
>>
>> The utility would then generate 2 .rb module files, such as:
>>
>> root.rb:
>> ======
>> require 'mymodule/sub'
>> module MyModule
>> class Root < SomeBaseClass
>> attr_accessor :sub_elements # Array
>> # Some more stuff
>> end
>>
>> sub.rb:
>> ======
>> module MyModule
>> class Sub < SomeBaseClass
>> attr_accessor :id
>> # Some more stuff
>> end
>> end
>>
>> The more I think about it, this kind of utility probably is
>> required to
>> analyze the DTD rather than the XML to get the proper
>> relationships. Any
>> such utility available?

>
> I really have no idea how your XML file above maps to the output you
> describe. It appears that you just look at the unique element names
> and create classes from them. You ignore the hierarchy of the XML
> file, you ignore all the attributes of the elements, and you ignore
> all the text content of the elements?


I think Jari might be confusing things with the test case he provided.
It seems to me that he has shown the nesting of elements/classes (e.g.
sub_elements -- the 'sub' is an element name) and attributes (e.g. the
'id' accessor -- the 'id' is an attribute name). He's missed the text
(and all that that implies), but he's looking for the tool not
promoting one so that's okay.

FWIW, my thing, xampl, generates 2 classes, 2 modules, and 59 methods
for Jari's example. Xampl defines a bunch of stuff to you work
with the generated code. Xampl doesn't use DTDs or schemas to do this.

Cheers,
Bob

>
>
> So, you just need something like this:
>
> raw_xml = IO.read( 'myfile.xml' )
> all_tag_names = raw_xml.scan( /<(\w+)/ ).flatten.uniq
> all_tag_names.each{ |tag_name|
> # use ERB here to create new files using
> # tag_name.capitalize as the foundation for the class name
> }
>


----
Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/



  Réponse avec citation
Vieux 23/11/2007, 15h24   #10
Jari Williamsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

Thanks, Bob! You understood despite my bad sample code.

By reading the description of your Xampl tool, this seems to be the tool
I need! In my case, analyzing a bunch of XML files like your tool do
would work as good as working on the DTD.

Best regards,

Jari Williamsson


Bob Hutchison wrote:

> On 22-Nov-07, at 10:34 PM, Phrogz wrote:
>
>> On Nov 22, 7:06 am, Jari Williamsson
>> <jari.williams...@mailbox.swipnet.se> wrote:
>>> Rolando Abarca wrote:
>>> It seems like I need to specify a bit better what I need: I'm looking
>>> for a utility that will _generate_ Ruby code (classes) from a
>>> hierarchical XML file.
>>>
>>> Let's say this is the XML data:
>>> mymodule.xml
>>> ======
>>> <root>
>>> <sub id="1">Some data</sub>
>>> <sub id="2>Some more data</sub>
>>> </root>
>>>
>>> The utility would then generate 2 .rb module files, such as:
>>>
>>> root.rb:
>>> ======
>>> require 'mymodule/sub'
>>> module MyModule
>>> class Root < SomeBaseClass
>>> attr_accessor :sub_elements # Array
>>> # Some more stuff
>>> end
>>>
>>> sub.rb:
>>> ======
>>> module MyModule
>>> class Sub < SomeBaseClass
>>> attr_accessor :id
>>> # Some more stuff
>>> end
>>> end
>>>
>>> The more I think about it, this kind of utility probably is required to
>>> analyze the DTD rather than the XML to get the proper relationships. Any
>>> such utility available?

>>
>> I really have no idea how your XML file above maps to the output you
>> describe. It appears that you just look at the unique element names
>> and create classes from them. You ignore the hierarchy of the XML
>> file, you ignore all the attributes of the elements, and you ignore
>> all the text content of the elements?

>
> I think Jari might be confusing things with the test case he provided.
> It seems to me that he has shown the nesting of elements/classes (e.g.
> sub_elements -- the 'sub' is an element name) and attributes (e.g. the
> 'id' accessor -- the 'id' is an attribute name). He's missed the text
> (and all that that implies), but he's looking for the tool not promoting
> one so that's okay.
>
> FWIW, my thing, xampl, generates 2 classes, 2 modules, and 59 methods
> for Jari's example. Xampl defines a bunch of stuff to you work with
> the generated code. Xampl doesn't use DTDs or schemas to do this.
>
> Cheers,
> Bob
>
>>
>>
>> So, you just need something like this:
>>
>> raw_xml = IO.read( 'myfile.xml' )
>> all_tag_names = raw_xml.scan( /<(\w+)/ ).flatten.uniq
>> all_tag_names.each{ |tag_name|
>> # use ERB here to create new files using
>> # tag_name.capitalize as the foundation for the class name
>> }
>>

>
> ----
> Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
> Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
> http://www.recursive.ca/ -- works on
> http://www.raconteur.info/cms-for-static-content/home/
>
>
>
>


  Réponse avec citation
Vieux 23/11/2007, 15h38   #11
Bob Hutchison
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: XML to Ruby?

Hi

On 23-Nov-07, at 10:24 AM, Jari Williamsson wrote:

> Thanks, Bob! You understood despite my bad sample code.
>
> By reading the description of your Xampl tool, this seems to be the
> tool I need! In my case, analyzing a bunch of XML files like your
> tool do would work as good as working on the DTD.


Great! If you need any , let me know and I'll do what I can. An
understatement would be "the documentation is sparse".

Cheers,
Bob

>
>
> Best regards,
>
> Jari Williamsson
>
>
> Bob Hutchison wrote:
>
>> On 22-Nov-07, at 10:34 PM, Phrogz wrote:
>>> On Nov 22, 7:06 am, Jari Williamsson
>>> <jari.williams...@mailbox.swipnet.se> wrote:
>>>> Rolando Abarca wrote:
>>>> It seems like I need to specify a bit better what I need: I'm
>>>> looking
>>>> for a utility that will _generate_ Ruby code (classes) from a
>>>> hierarchical XML file.
>>>>
>>>> Let's say this is the XML data:
>>>> mymodule.xml
>>>> ======
>>>> <root>
>>>> <sub id="1">Some data</sub>
>>>> <sub id="2>Some more data</sub>
>>>> </root>
>>>>
>>>> The utility would then generate 2 .rb module files, such as:
>>>>
>>>> root.rb:
>>>> ======
>>>> require 'mymodule/sub'
>>>> module MyModule
>>>> class Root < SomeBaseClass
>>>> attr_accessor :sub_elements # Array
>>>> # Some more stuff
>>>> end
>>>>
>>>> sub.rb:
>>>> ======
>>>> module MyModule
>>>> class Sub < SomeBaseClass
>>>> attr_accessor :id
>>>> # Some more stuff
>>>> end
>>>> end
>>>>
>>>> The more I think about it, this kind of utility probably is
>>>> required to
>>>> analyze the DTD rather than the XML to get the proper
>>>> relationships. Any
>>>> such utility available?
>>>
>>> I really have no idea how your XML file above maps to the output you
>>> describe. It appears that you just look at the unique element names
>>> and create classes from them. You ignore the hierarchy of the XML
>>> file, you ignore all the attributes of the elements, and you ignore
>>> all the text content of the elements?

>> I think Jari might be confusing things with the test case he
>> provided. It seems to me that he has shown the nesting of elements/
>> classes (e.g. sub_elements -- the 'sub' is an element name) and
>> attributes (e.g. the 'id' accessor -- the 'id' is an attribute
>> name). He's missed the text (and all that that implies), but he's
>> looking for the tool not promoting one so that's okay.
>> FWIW, my thing, xampl, generates 2 classes, 2 modules, and 59
>> methods for Jari's example. Xampl defines a bunch of stuff to
>> you work with the generated code. Xampl doesn't use DTDs or schemas
>> to do this.
>> Cheers,
>> Bob
>>>
>>>
>>> So, you just need something like this:
>>>
>>> raw_xml = IO.read( 'myfile.xml' )
>>> all_tag_names = raw_xml.scan( /<(\w+)/ ).flatten.uniq
>>> all_tag_names.each{ |tag_name|
>>> # use ERB here to create new files using
>>> # tag_name.capitalize as the foundation for the class name
>>> }
>>>

>> ----
>> Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
>> Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
>> http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/

>


----
Bob Hutchison -- tumblelog at http://www.recursive.ca/so/
Recursive Design Inc. -- weblog at http://www.recursive.ca/hutch
http://www.recursive.ca/ -- works on http://www.raconteur.info/cms-for-static-content/home/




  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26011 seconds with 19 queries