简体   繁体   English

从XSD生成Ruby类

[英]Generate Ruby Classes from XSD

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? 有没有办法从XSD生成Ruby类(甚至可能是ActiveResource类),以便它们包含一种将类序列化为xml对初始XSD有效的方法?

I know that soap4r has xsd2ruby but it appears that the generated ruby classes cannot be easily serialized to xml. 我知道soap4r有xsd2ruby,但似乎生成的ruby类不能轻易地序列化为xml。

Shameless self promotion (hope this is okay on stackoverflow) but I'm working on an open source project to do just that 无耻的自我推销(希望这在stackoverflow上没问题),但我正在开发一个开源项目来做到这一点

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. 它仍然是一个正在进行的工作(随意发送补丁),但最终目标是将XSD转换为Ruby类(现在它),并将符合XSD的XML转换为这些类的实例。

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. 我有一个同事的.xsd,想从中生成一个类文件。 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. 我的希望是,我将能够轻松地对对象进行编组并将其传递给他的RESTful端点,在那里他的Java服务器将解组有效负载并动态地在他身边构建对象而无需额外的努力。

The solution I found was to get the soap4r from https://github.com/rubyjedi/soap4r . 我找到的解决方案是从https://github.com/rubyjedi/soap4r获取soap4r I made the two *.rb files in the bin directory executable and then ran: 我在bin目录中生成了两个* .rb文件,然后运行:

bin/xsd2ruby.rb --xsd <source>.xsd --classdef <filename_prefix>

This generated a new file with each of the xsd:complexType implemented as a class. 这生成了一个新文件,其中每个xsd:complexType实现为一个类。 All other complex type were also generated with the correct inheritance relationships and all xsd:element was defined as an instance variable and a class initializer also defined. 所有其他复杂类型也使用正确的继承关系生成,并且所有xsd:element被定义为实例变量,并且还定义了类初始化程序。

Running xsd2ruby.rb by itself yielded the options: xsd2ruby.rb运行xsd2ruby.rb会产生以下选项:

~/src/test/soap4r:bin/xsd2ruby.rb 
Usage: bin/xsd2ruby.rb --xsd xsd_location [options]
  xsd_location: filename or URL

Example:
  bin/xsd2ruby.rb --xsd myapp.xsd --classdef foo

Options:
  --xsd xsd_location
  --classdef [filenameprefix]
  --mapping_registry
  --mapper
  --module_path [Module::Path::Name]
  --force
  --quiet

For the sake of completeness, I extended my class with the following (this is a "Prospect" class): 为了完整起见,我用以下内容扩展了我的课程(这是一个“Prospect”课程):

class Prospect
  include Enumerable
  def each(&block)
    self.instance_variables.collect{|v| (v.gsub /@/, '').to_sym }.each(&block)
  end
end

This let me use it as the body of an Net::HTTP::Post request. 这让我用它作为Net::HTTP::Post请求的主体。

To the question of a free to_xml : I haven't found it. 对于免费的to_xml :我还没找到它。 The ruby Object comes with a to_yaml and to_json out of the box, but I've not found any simple conversion to XML. ruby对象带有一个to_yaml即用的to_yamlto_json ,但我没有找到任何简单的XML转换。 So it came down to a roll my own "to_xml". 所以它归结为我自己的“to_xml”。

Hope this helps. 希望这可以帮助。

It appears that this might work. 看起来这可能有效。

require 'xsd/mapping' XSD::Mapping.obj2xml(xsdBasedObject) require'xdd / mapping'XSD :: Mapping.obj2xml(xsdBasedObject)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM