从 XSD 生成 PHP 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2263771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
generate PHP classes from XSD?
提问by Qwerty
Is there in the world analogues of JavaBeans or JAXB for PHP? Is it possible to generate PHP classes from XML schema?
世界上是否有用于 PHP 的 JavaBeans 或 JAXB 的类似物?是否可以从 XML 模式生成 PHP 类?
It's common practice to publish API's as XSD schemas. Java and C# guys can get advantage of this by generating classes right from XSD. Is there same tool for PHP?
将 API 发布为 XSD 模式是常见的做法。Java 和 C# 人员可以通过直接从 XSD 生成类来利用这一点。PHP 有相同的工具吗?
回答by Mike Bevz
I'm working now on this issue and going to release the tool as soon as it reaches more-less stable state. Check here http://mikebevz.com/xsd-to-php-tool/
我现在正在处理这个问题,并会在工具达到更不稳定的状态时立即发布。在这里查看http://mikebevz.com/xsd-to-php-tool/
Upd. I've just release first working prototype, it works fine with UBL 2.0 schemas and one simple schema, but more serious testing is on the way. I'd appreciate if you send schemas you're working with, so I'd include them in the test suite.
更新。我刚刚发布了第一个工作原型,它适用于 UBL 2.0 模式和一个简单的模式,但更严格的测试正在进行中。如果您发送正在使用的模式,我将不胜感激,因此我会将它们包含在测试套件中。
Upd. 2. XSD2PHP reached version 0.0.5. Check the progress on https://github.com/moyarada/XSD-to-PHP
更新。2. XSD2PHP 达到 0.0.5 版本。检查https://github.com/moyarada/XSD-to-PHP上的进度
回答by Martin Wickman
The main reasons for using XSD class generators is to
使用 XSD 类生成器的主要原因是
- Get compile time checking
- An easier syntax than plain old XML API's
- Auto completion in your IDE.
- 获取编译时检查
- 比普通的旧 XML API 更简单的语法
- 在您的 IDE 中自动完成。
Now contrast this with PHP. PHP does nothave compile time checking and it hassupport for dynamic methods/properties. This voids two of the main reasons above and makes this a non-issue unless you reallyneed auto completion. In other words, there is reason to use an XSD class generator in PHP, and that is probably also why none exist.
现在将其与 PHP 进行对比。PHP并不会有编译时检查和它具有的动态方法/属性的支持。这使上述两个主要原因无效,并且除非您确实需要自动完成,否则这不是问题。换句话说,有理由在 PHP 中使用 XSD 类生成器,这可能也是不存在的原因。
My suggestion is to use PHPs Simple XMLwhich creates properties to match the XML dynamically during runtime. If you validate your XML against the XSD file and then create a Simple XML object, you have your XML object structure complete with methods and properties, without having to generate code. A perfectly good approach in PHP.
我的建议是使用 PHP 的Simple XML,它创建属性以在运行时动态匹配 XML。如果您根据 XSD 文件验证您的 XML,然后创建一个简单的 XML 对象,您的 XML 对象结构就具有完整的方法和属性,而无需生成代码。PHP 中一个非常好的方法。
Note that I don't state that SimpleXML is the same as generated XSD classes, of course not.. But it is pretty close, usage and API-wise. You still end up doing something like $company->employee[2]->firstnameeither way.
请注意,我没有声明 SimpleXML 与生成的 XSD 类相同,当然不是。. 但它非常接近,使用和 API 方面。$company->employee[2]->firstname无论哪种方式,您最终仍然会做类似的事情。
回答by Martin Taleski
XSD schemas are usually written in WSDL files on SOAP Web Services.
XSD 模式通常写在 SOAP Web 服务上的 WSDL 文件中。
wsdl2phpis a tool for parsing WSDL(XSD) schemas to php classes. It uses php's native SoapClient as it's client:
wsdl2php是一个用于将 WSDL(XSD) 模式解析为 php 类的工具。它使用 php 的原生 SoapClient 作为它的客户端:
回答by Boy Baukema
There is another recent tool called PiBXa JiBXinspired tool.
From the site:
从网站:
PiBX is an XML-Data-Binding framework for PHP.
With PiBX you can generate PHP classes based off an available XML-Schema. These classes can be used to marshal the informations to XML without hassling with schema checks, constraints or restrictions.
PiBX 是一个用于 PHP 的 XML 数据绑定框架。
使用 PiBX,您可以根据可用的 XML-Schema 生成 PHP 类。这些类可用于将信息编组为 XML,而无需进行模式检查、约束或限制。
回答by Tim Ramsey
This seems to do a decent job https://github.com/goetas-webservices/xsd2php
这似乎做得不错https://github.com/goetas-webservices/xsd2php
I wish it handled enumeration validation, but seemed to work ok in my use case. I found the META .yml files it generates helpful.
我希望它处理枚举验证,但在我的用例中似乎工作正常。我发现它生成的 META .yml 文件很有帮助。
回答by Sebastian Viereck
This library seems to be the best choice nowadays: https://github.com/goetas/xsd2php
这个库现在似乎是最好的选择:https: //github.com/goetas/xsd2php
It generates PHP classes for XML Elements and can convert it back and forth:
它为 XML 元素生成 PHP 类并且可以来回转换它:
XML -> PHP -> XML
XML -> PHP -> XML
回答by xcut
I looked into that a while ago, and I certainly could not find one. If your schema is simple, there's a guy who hacked a simple version togetherfor flat schemas.
我不久前调查过,我当然找不到。如果您的模式很简单,那么有人会为平面模式编写一个简单的版本。
That's all I know about. Normally these guysare good at supporting languages other than the main ones, but they don't do PHP either.
这就是我所知道的。通常这些家伙擅长支持主要语言以外的语言,但他们也不支持 PHP。
回答by Ira Baxter
The DMS Software Reengineering Toolkitis configurable code generation machinery, that can be used to process arbitrary formal documents as input. DMS can be used to generate code in arbitary output languages.
在DMS软件再造工具包是可配置的代码生成机制,可以用来处理任意的正式文件作为输入。DMS 可用于生成任意输出语言的代码。
We have used it to generate native Java and COBOL XML readers and writers from DTDs, which are the elder cousin of schemas. The same ideas would be easily applied to PHP.
我们已经使用它从 DTD 生成本机 Java 和 COBOL XML 读取器和写入器,它们是模式的老表亲。相同的想法很容易应用于 PHP。
回答by Rui Curado
XML is an awkward syntax to work on. There are code generators that use XML as input, but unless XML is your only choice don't go this route. I am assuming you want to generate your code while developing, and then upload it to a server.
XML 是一种难以处理的笨拙语法。有些代码生成器使用 XML 作为输入,但除非 XML 是您唯一的选择,否则不要走这条路。我假设您想在开发时生成代码,然后将其上传到服务器。
Then, you will probably need to target multiple languages (PHP + JavaScript/AJAX + HTML). So it would be better to use a multi-target code generator, specially if the generator can target multiple assets from a single definition (for instance, from a database table definition, create SQL code, HTML pages for CRUD operations, and any JS support code).
然后,您可能需要针对多种语言(PHP + JavaScript/AJAX + HTML)。因此最好使用多目标代码生成器,特别是如果生成器可以从单个定义(例如,从数据库表定义、创建 SQL 代码、用于 CRUD 操作的 HTML 页面以及任何 JS 支持)中针对多个资产代码)。
I am currently working on a code generator that does multi-targetting. No product available yet but you can check the methodology behind it at http://www.abse.info.
我目前正在研究一个多目标的代码生成器。尚无可用产品,但您可以在http://www.abse.info 上查看其背后的方法。

