views:

359

answers:

2

I have an XML schema that defines my data model. I would now like to have Objective C source files generated from the XML schema. Does anyone know how to accomplish this?

+1  A: 

Take a look at this Stack Overflow question on XML serialization, which mentions a project along these lines.

Alex Reynolds
I think that is more geared to creating instances of objects rather than creating the classes in the first place.
vickirk
Yes, but it might be easier to extend than to code from scratch.
Alex Reynolds
A: 

Without knowing the details my immediate thought would be to probably use xslt for this. e.g. if you had something like (I appreciate

<element name="SomeEntity">
  <attribute name="someAttr" type="integer" />
  <complexType>
    <sequence>
      <element name="someOtherAttr" type="string" />
    </sequence>
  </complexType>
</entity>

Create a bunch of templates to translate this,e.g.

<xsl:template match="element">
  <xsl:apply-template select="." mode="header"/>
  <xsl:apply-template select="." mode="impl"/>
</xsl:template>

<xsl:template match="element" mode="header">
class <xsl:value-of select="@name"/> {
 public:
  <xsl:apply-template select="attribute" mode="header"/>

  <xsl:apply-template select="complexType/element" mode="header"/>
</xsl:template>

...

Though if the logic on the generation is more complex I would probably go down the road of importing the xml into an object model and programmatically process that, possibly using a template engine such as Velocity, as while it is possible complex logic in xslt is a pain.

vickirk