Justin Kredible
Justin Kredible

Reputation: 8414

How to generate Objective C class files from XML schema?

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?

Upvotes: 4

Views: 4281

Answers (2)

vickirk
vickirk

Reputation: 4067

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.

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96927

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

Upvotes: 1

Related Questions