generate PHP classes from XSD?

后端 未结 8 2129
春和景丽
春和景丽 2020-12-05 07:41

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

相关标签:
8条回答
  • 2020-12-05 07:51

    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.

    0 讨论(0)
  • 2020-12-05 07:56

    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.

    0 讨论(0)
  • 2020-12-05 07:58

    The main reasons for using XSD class generators is to

    1. Get compile time checking
    2. An easier syntax than plain old XML API's
    3. 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.

    0 讨论(0)
  • 2020-12-05 07:59

    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.

    0 讨论(0)
  • 2020-12-05 08:00

    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:

    https://github.com/jbarciauskas/wsdl2php

    0 讨论(0)
  • 2020-12-05 08:01

    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/

    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

    0 讨论(0)
提交回复
热议问题