DTD2CPP is a small Perl script that uses a DTD to generate C++ headers and source to load XML files conforming to that DTD. The generated classes have names matching the <!ELEMENT> fields from the DTD, likewise child nodes are named as expected. Only a subset of possible DTDs are supported due to these naming restrictions.

This means that you can navigate an XML data structure naturally as if it were a native C++ data structure. The SourceForge Summary page allows you to download the perl script as well as an example DTD that illustrates the operation of the script. View the README.

libxml2 is used to perform actual XML I/O. The generated code is only a C++ binding to a specific DTD.

Type Conversions

The parser understands a comment in the DTD of the form <!--TYPE ... --> containing type specifications (eg int, float, string, arrays thereof) for each property of an node or #PCDATA child node. See the script and example DTD for how this works.


An extremely limited subset of DTDs is supported - both because of the way that XML data structures are mapped to C++ (some ordering information may be lost) and because the DTD parser is very naive.

(a|b)* type declarations are now supported as of the 20060420 release. Read the documentation for how to use this feature, it requires that A and B both be subclasses of some common superclass and that you write (superclass*) into the DTD.

There is no mechanism currently for output. Only input is supported.

Help Wanted

If you are a Perl Guru (I am not), feel free to improve this script in any way you see fit. Patches submitted to me will almost certainly be accepted if they work with my test DTDs. In particular, a more formal DTD parsing algorithm would be welcome - the code in that department is currently very poor.

If you're a web developer (I am not), you'll note that this page bites. If you are offended by it, I also welcome any submissions of HTML :)