views:

1630

answers:

7

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?

A: 

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.

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).

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.

Rui Curado
Your stuff is more like meta programming language, I don't need that. 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. I want the same tool for PHP.
Qwerty
A: 

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.

xcut
A: 

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.

Ira Baxter
Thanks, but I guess it's quite expensive :-)
Qwerty
+1  A: 

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.

Martin Wickman
Qwerty
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 :-)
Martin Wickman
A: 

I don't have an answer but I need exact same thing! I come from .NET camp and I used XSD.exe to generate C# classes that can be used to easily serialize/deserialize to/from XML files for many projects. Now I have to build PHP/MySQL stack and I want to use the same technique.

I will come back to this question, hoping that there exist a solution for PHP. Sorry this isn't help you but I just wanted to echo the need for it.

yoshi
A: 

Hey,

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.

Mike Bevz
A: 

Perhaps the right technology is SDO XML Data Access Service. Summarizing the PHP documentation in brief (and ignoring proper exception handling), here's how you'd load a schema, set some fields, and then export as XML.

$xmldas = SDO_DAS_XML::create("schema.xsd");
$doc = $xmldas->createDocument();
$rdo = $doc->getRootDataObject();
$rdo->nameFirst = "Brady";
$rdo->nameLast = "Doverspike";
echo $xmldas->saveString($doc, 4);
jabbett