Is there a way to generate Ruby classes (maybe even ActiveResource classes) from an XSD so that they contain a way to serialize the classes to xml valid for the initial XSD?
I know that soap4r has xsd2ruby but it appears that the generated ruby classes cannot be easily serialized to xml.
Mark,
Though this was asked a while ago, I came across a solution and thought it might help folks in the future.
My need was similar. I have a .xsd from a colleague and would like to generate a class file from it. My hope is that I'll be able to easily marshall the object and pass it to his RESTful end-point, where his Java server will unmarshall the payload and dynamically build the object on his side with no additional effort.
The solution I found was to get the
soap4r
from https://github.com/rubyjedi/soap4r. I made the two *.rb files in the bin directory executable and then ran:This generated a new file with each of the
xsd:complexType
implemented as a class. All other complex type were also generated with the correct inheritance relationships and allxsd:element
was defined as an instance variable and a class initializer also defined.Running
xsd2ruby.rb
by itself yielded the options:For the sake of completeness, I extended my class with the following (this is a "Prospect" class):
This let me use it as the body of an
Net::HTTP::Post
request.To the question of a free
to_xml
: I haven't found it. The ruby Object comes with ato_yaml
andto_json
out of the box, but I've not found any simple conversion to XML. So it came down to a roll my own "to_xml".Hope this helps.
It appears that this might work.
require 'xsd/mapping' XSD::Mapping.obj2xml(xsdBasedObject)
Shameless self promotion (hope this is okay on stackoverflow) but I'm working on an open source project to do just that
Its still a work in progress (feel free to send patches) but the ultimate goal is to convert XSD to/from Ruby classes (which it does now) and convert XML conforming to that XSD to/from instances of those classes.