November 8th, 2004, 12:33 PM
XML schema binding to PHP code (jaxb equivalent?)
anybody aware of a project to implement something similar to Java's JAXB (http://java.sun.com/xml/jaxb/about.html)?
as i understand it, generates code on-the-fly from an XML schema such as XSD doc. also does marshalling/unmarshalling and data validation. a use case for elaboration would be:
- read in an XSD document
- appropriate classes are generated on the fly
- coder instantiates objects from those classes, calls accessor methods to fill in data
- finally make a call to marshal the data into an XML document
- able to unmarshal that same XML document into replicas of the original objects
is something like that out there? anybody interested in such a project?
November 8th, 2004, 05:03 PM
There is not anything like this to my knowledge for PHP. Basically you want to feed a script an XSD and have it create a class to marshal/unmarshal XML data. PHP supports WSDL, XML and XSLT but there are no XSD extensions, so you'd be looking at undertaking a fairly sizeable project.
I'd be interested if you're paying - drop me a line on msn messenger, email@example.com or on AIM, laynestaley02 if you want to talk.
If my post helped you, please click the
above my post and leave a comment. Thanks
November 8th, 2004, 06:22 PM
your payoff would be the knowledge of a job done well, creating a useful tool for the community i've got no money for you, i'm just some guy.
November 28th, 2004, 06:03 AM
Originally Posted by n.eby
Well I was also looking for the same thing so it would be very helpful if we could share our findings.
Mail me at firstname.lastname@example.org
August 14th, 2006, 08:11 PM
Nearly 2 years later and I came back around to the source of my original post on this topic. I still want a fully featured pure-PHP-code XML schema binding facility. There is no project out there; AFAIK there is also no PHP extension for such a project.
Anyway, here is my solution for the time being. It is a long ways short of being able to bind even a fraction of the breadth of possible XML schemas. Really it's only good for one very narrow use case that I'll describe below. I may expand on it, I may not, but there it is.
This works if your schema follows some conventions in my narrow use case:
- contains some complexType nodes that you want to turn into classes named after the containing <element>
- a complexType can have <attribute ref=""> referring by name to global <attribute> nodes
- a complexType can have either <simpleContent> or <sequence>
- <sequence> can have <group ref=""> referring by name to global <group> nodes; <element ref=""> referring by name to global element nodes; and <choice> nodes.
Classes bound to complexType with sequences will have protected members for every <element> and <attribute> contained in the <complexType>, and for any <element> in any referred-to <group>. a camelCase naming convention is used for the class members.
Classes bound to complexType nodes containing only a simpleContent node, will have a protected member called $value.
The bound classes are, in my design, supposed to extend a class that provides automatic accessor methods for any public or protected member of child instances. So protected $foo will correspond with getFoo() and setFoo(). This is fairly academic to implement using php5's overloading features but if you want to see it, post a message here and I will probably notice and I can find and post the code.
Usage for this class, which is called XmlSchemaBinding: assuming mySchema.xsd has a complexType-containing element with name like "the-struct" or "theStruct". A new class called TheStruct will be created and put in scope. If you specify the base-class parameter to constructor, TheStruct class will extend that class.
Code is attached. Yes it uses eval(), so sue me.
$binding = new XmlSchemaBinding("/path/to/mySchema.xsd"[, "XsdBaseClass"]);
$struct = new TheStruct();
$xml = $binding->marshalXml($struct);