1

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
  • I would suggest mapping xs:string to a better construct, std::string if you are using C++ or a couple char* and int if you want C compatibility. Commented Dec 19, 2009 at 13:41
  • Do you mean: how to map a C++ abstract syntax tree to an XML structure? Commented Nov 6, 2010 at 9:04

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

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,
0

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">

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

2 Comments

thanks for reply? then ho to map C/C++ code? can you give an exmple/ thanks again
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.