'generate PHP classes from XSD? [closed]
Is there in the world analogues of JavaBeans or JAXB for PHP? Is it possible to generate PHP classes from XML schema?
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?
Solution 1:[1]
I'm working now on this issue and going to release the tool as soon as it reaches more-less stable state. Check here https://web.archive.org/web/20111026063725/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.
Upd. 2. XSD2PHP reached version 0.0.5. Check the progress on https://github.com/moyarada/XSD-to-PHP
Solution 2:[2]
The main reasons for using XSD class generators is to
- Get compile time checking
- An easier syntax than plain old XML API's
- Auto completion in your IDE.
Now contrast this with PHP. PHP does not have compile time checking and it has support for dynamic methods/properties. This voids two of the main reasons above and makes this a non-issue unless you really need auto completion. In other words, there is reason to use an XSD class generator in PHP, and that is probably also why none exist.
My suggestion is to use PHPs Simple XML which 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.
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]->firstname either way.
Solution 3:[3]
This seems to do a decent job 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.
Solution 4:[4]
XSD schemas are usually written in WSDL files on SOAP Web Services.
wsdl2php is a tool for parsing WSDL(XSD) schemas to php classes. It uses php's native SoapClient as it's client:
Solution 5:[5]
This library seems to be the best choice nowadays: https://github.com/goetas/xsd2php
It generates PHP classes for XML Elements and can convert it back and forth:
XML -> PHP -> XML
Solution 6:[6]
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 together for flat schemas.
That's all I know about. Normally these guys are good at supporting languages other than the main ones, but they don't do PHP either.
Solution 7:[7]
The DMS Software Reengineering Toolkit is 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.
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.
Solution 8:[8]
There is another recent tool called PiBX a JiBX inspired 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | artlung |
| Solution 2 | |
| Solution 3 | Tim Ramsey |
| Solution 4 | |
| Solution 5 | |
| Solution 6 | xcut |
| Solution 7 | Ira Baxter |
| Solution 8 | Boy Baukema |
