generate PHP classes from XSD?

40,550

Solution 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

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.

Solution 3

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

Solution 4

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.

Share:
40,550

Related videos on Youtube

Qwerty
Author by

Qwerty

Updated on July 09, 2022

Comments

  • Qwerty
    Qwerty almost 2 years

    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?

    • rpiaggio
      rpiaggio over 14 years
      Well, there is always a way ... whether or not it is practical or not is a whole other ball game. I can imagine writing a class that would read a XML file and put together a string to declare a class and its methods, and then eval it.
    • Qwerty
      Qwerty over 14 years
      Do you mean generating at run-time? It would be more practical to get just a bunch of ordinary classes (and have auto-completion working)
    • Martin Taleski
      Martin Taleski over 12 years
      try out wsdl2php as I have stated in the answer. It is used for parsing wsdl to php classes but I think wsdl and xsd should be the same thing.
  • Qwerty
    Qwerty about 14 years
    Thanks, but I guess it's quite expensive :-)
  • Qwerty
    Qwerty about 14 years
    Absolutely wrong. SimpleXML is just generic XML object, it has nothing in common with data types described in XSD. I need to create business logic entities from XSD and operate on them in code, it's not just about creating & validating XMLs.
  • Martin Wickman
    Martin Wickman about 14 years
    I never said SimpleXML was the same as XSD classes. Please read and think about what I am saying about dynamic/static and me arguing that XSD generated classes in PHP is pretty meaningless. It's a workaround for a non-issue. So instead, I gave you another approach which is more in the spirit of PHP. But if you're still determined to find a solution to the I-want-Java-XMLBeans-in-PHP approach, best of luck to you :-)
  • jnrbsn
    jnrbsn over 12 years
    I checked this out (tried to compile the SAML2 XSD with it) and there seems to be no progress since February '11, there is a fork which fixes the most obvious flaws (without which it won't run) but it still needs some work...
  • Mike Bevz
    Mike Bevz over 12 years
    Guys, come on. It's open source - you take the source and fix it. Instead I get downvoted. I don't get it.
  • Chad Wilson
    Chad Wilson about 12 years
    And what happens when the XSD changes? I disagree with your assertion that that list makes up the only reasons for doing Object <-> XML marshalling/unmarshalling. Most places that are heavily using web services, or are exchanging standards based data use the XSD as the single point of truth. The power of having the ability to map back and forth to code is that you can create language specific constructs with one normative source of truth.
  • Thomas Keller
    Thomas Keller about 10 years
    This software seems to be officially abandoned by the author.
  • Shahriyar Imanov
    Shahriyar Imanov almost 10 years
    Don't bloody touch SimpleXML, it's garbage when it comes to serious XML work. I had to write my own PHP libs with \DOM library to have a decently optimized XML processing.
  • Erenor Paz
    Erenor Paz over 7 years
    Page is suspended. Link does not work anymore, unfortunately