views:

307

answers:

4

What the best way to map XML schemas to C/C++?

Here is an example:

------ C/C++ -----

    struct zone {
       char *var_name;
       float var_value;
    };

------ XML -----

    <xs:element name="zone">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Var_name" type="xs:string"/>
          <xs:element name="var_value" type="xs:decimal"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
+2  A: 

Xerces works pretty well as a library for pulling in the XML document. (You didn't say what OS or dev environment so this is pretty generic C and C++.)

From there a struct/class per element would be a good mapping. If you have a lot of XML elements to parse, I'd try looking for, or writing, a code generator to pull in the XML and spit out your class or struct definitions. Once you get it right once, doing it for any XML element is a piece of cake.

John at CashCommons
Thanks for your reply. Actually I am working on solaris with gcc and windows xp with vc++6. As I have got two programs (one works with xml and other with c++), I am tying to couple them to run some simulation studies. Thanks again for your help,
make
A: 

Code generation from XML such as this is best achieved using XSLT. If you have libxslt installed you can use xsltproc to perform the transform. Make this a pre-build step in your build process to generate the source code.

How about this:

structs.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

<xs:element name="zone">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Var_name" type="xs:string"/>
      <xs:element name="var_value" type="xs:decimal"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="zone2">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Var_name" type="xs:string"/>
      <xs:element name="var_value" type="xs:decimal"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

makestructs.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:for-each select="/xs:schema/xs:element">
struct <xsl:value-of select="@name" /> {
            <xsl:for-each select="xs:complexType/xs:sequence/xs:element">
                <xsl:choose>
                    <xsl:when test="@type = 'xs:string'">
    char*
                    </xsl:when>
                    <xsl:when test="@type = 'xs:decimal'">
    float
                    </xsl:when>
                </xsl:choose>
            <xsl:value-of select="@name" />;
            </xsl:for-each>
};
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The stylesheet is indented for readability. but you'll want to remove some whitespace so it doesn't appear in the output.

drspod
thanks for reply? then ho to map C/C++ code? can you give an exmple/ thanks again
make
You mean that you have C struct definitions that you want to map to XML schemas? The question suggested the inverse so perhaps you should clarify.
drspod
+2  A: 

CodeSynthesis XSD is an XML Schema to C++ compiler that does pretty much exactly what you are looking for. If you want a more lighter-weight version, there is also XSD/e which is geared more towards mobile/embedded development.

Boris Kolpackov
thanks a lot! that is great, I would try it ... thanks again
make