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
}