34

I have to write a C++ Application (using the Qt Framework for the GUI) that can edit data stored in xml files described by a xsd schema file. Is there a tool to convert the xsd schema into C++ Classes?

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
Andre
  • 3,223
  • 4
  • 23
  • 19

8 Answers8

29

Sounds to me like CodeSynthesis is exactly what you are looking for. It's open source and c++.

mickus
  • 366
  • 2
  • 6
  • 26
    It's worth noting that *open source* is a bit tricky here. The code generated by *CodeSynthesis* is also covered by GPL license, so you can't use it in your application without distributing the source code of your whole application. There are exceptions for [low volume usage](http://www.codesynthesis.com/products/xsd/free-license.xhtml) or proprietary license to buy though. – Jarekczek Nov 04 '12 at 08:12
  • I cannot use CodeSynthesis in VS2019 – Nguyễn Đức Tâm Nov 29 '19 at 07:10
8

See XmlPlus-xsd2cpp at Google:

XmlPlus xsd2cpp provides "simple to use" C++ XML data-binding through W3C XML-Schema.

Usage of XmlPlus is covered by the GNU Lesser General Public License

DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • 4
    Please note that the tool is not BY Google, it is just hosted at Google Code. – Beginner Oct 10 '14 at 07:50
  • Looks promising. Unfortanetely xsd2cpp is not supporting every xsd specification :/ e.g. attributeGroups are not supported. – ruuns Oct 27 '19 at 09:56
  • The tool use `Expat` parser that was based on SAX (stream parsing with callbacks), which made parsing some documents like COLLADA very inconvenient. ref to : [installation institution](http://xmlplus.sourceforge.net/) – Kevin Chou Sep 14 '21 at 03:56
6

gSOAP Toolkit can do this too! Its is lightweight, and supports C/C++. I have already used it in very demanding projects with success. Also, its licensed under GPL2.

Portability: gSOAP supports most platforms, including embedded systems and small OS (for example WinCE, Symbian, and PalmOS). Portability is tested for Windows (98, XP, Vista), Linux, Unix, Mac OS X, Solaris, HP-UX, AIX, FreeBSD, TRU64, Irix, QNX, and VxWorks.

bhelm
  • 695
  • 1
  • 7
  • 14
5

Altova XML Spy can generate C++ from an XSD, it's commercial but there's a 30 day free trial if you want to try it out.

Tom
  • 43,583
  • 4
  • 41
  • 61
4

Objective Systems, Inc. XBinder XML Schema Compiler (not only für C++).

Raphael Bossek
  • 1,904
  • 14
  • 25
3

Codalogic LMX is also an option.

2

There's a Microsoft tool which does this, I think, called xsd.exe (but I haven't tried it myself).

ChrisW
  • 54,973
  • 13
  • 116
  • 224
0

All the generators are absolutely terrible.

XSD describes a class hierarchy in which classes contain subclasses, which may contain other subclasses and all you want to do is represent it in the same way.

For example if this is your schema:

<xs:element name="shipto">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element> 

You'd want to produce something like:

class shipTo
{
  private:
    string name;
    string address;
    string city;
    string country;

  public:
    set_Name();
    get_Name();
    ...
}

You're not going to find it. The closest thing I have found is xjc, which is for Java.

You'd expect something as BASIC as this functionality would exist, but I haven't found it yet, and yes, I have used Altova XML-Spy. I'm seriously surprised anybody would suggest this as a code generator. Its generated code is absolutely awful.

I'm writing a lex/bison parser to do this for my project because all the tools I've been able to find so far produce fairly horrible code. Altova has a 30 day trial period, if you don't believe me, try it. It is easier to write a lex/bison parser for my XSD than it is to use a $500 professional code package that produces a terrible class representation.

I can't believe people make use of XML in C++ because the tools for it are terrible.

user6269400
  • 129
  • 1
  • 2
  • Can you show us an example of what the "bad tools" produce and explain why you think it is bad? – Ira Baxter Jul 12 '16 at 19:09
  • Can't in a response since I'm stuck in "mini-Markdown formatting" here. I gave you an example schema - go ahead and run the Altova XML spy on it and compare that to the example MY hand rolled XSD->C++ lex/bison/hack produces. Go ahead. I am not the LEAST impressed with the tools available. XJC does produce fairly good code, however, it's in Java so it's not suitable for me. I feel like I'm working with stone knives and bearskins. – user6269400 Jul 12 '16 at 23:18
  • Don't have Altova spy... do see your proposed class structure and agree it looks reasonable. Maybe you can explain in 1-2 sentences what Altova does that you dislike. – Ira Baxter Jul 12 '16 at 23:30
  • Well, my AltovaSpy trial license is expired, and although I could backdate my computer to make it work, I'm not going to bother. If you really want to know how terrible the tools are, just get the trial license. Altova was alright at visual representation. I can say this, it's extremely verbose, they make extensive use of extremely long prefix names, they use their own internally defined types instead of obvious things like "string", or even "int", data isn't stored in the class itself, but instead inside of a stream so this amounts to a parser not a set of containers. – user6269400 Jul 12 '16 at 23:50
  • Is the use of streams intended to handle the problem of really huge XML files? – Ira Baxter Jul 13 '16 at 02:23
  • I don't know, but the end result is you have a bunch of classes which are very difficult to work with. – user6269400 Jul 13 '16 at 17:39