Re: [xml] code generation for C++

On Thu, 2003-11-20 at 01:34, William Brodie-Tyrrell wrote:
(warning: xml n00b)

IIRC, the xmlroff project at SourceForge had something like this, though
it generates GObjects in C, rather than C++ classes.

In addition, I've hacked up something that does something a bit like
this, though mine is for C, and you have to manually describe the
doctype using a custom set of C preprocessor macros.

It lives inside conglomerate/src in GNOME's CVS; a set of "gxx"
headers.  There are several headers, you #include one, then #include a
header containing DTD <-> C struct mappings, then repeat with a
different header.  One header generates a loader from XML to C, another
generates a saver from C to XML, and another generates prototypes.

It's an evil bit of hackery, and only does what I need so far, but it
saves tediously writing it all by hand and has fixed some bugs for me.

Please let me know if you find other alternatives.

Can anyone point me at a package that will generate c++ headers and code to
represent (and load using libxml2) a data structure defined by a DTD?  I
hacked one up in Perl but its parser is extremely brain-dead and so supports
only a very limited subset of possible DTDs (it can't handle much more than
the following example).

I want it to be able to take a DTD like this:

<!ELEMENT a (b,c*)>
      name CDATA #REQUIRED
      place CDATA #REQUIRED
      param (false|true) "false">

and generate classes for a, b & c, eg:

class xml_mydtd_a {
      xml_mydtd_a(xmlDocPtr, xmlNodePtr);
      virtual ~xml_mydtd_a();

      xml_mydtd_b *b;
      std::vector<xml_mydtd_c *> c;
      std::string name;
      std::string place;

class xml_mydtd_b {
      xml_mydtd_b(xmlDocPtr, xmlNodePtr);
      virtual ~xml_mydtd_b();

      enum param_ENUM {
      } param;

The generated code should be able to load each of those object types into
instances of the appropriate classes, allowing me to access them sensibly:

xmlDocPtr doc = xmlParseFile(docname);
xmlNodePtr cur = xmlDocGetRootElement(doc);
xml_mydtd_a a = new xml_mydtd_a(doc, cur);

cout << a->name << " at " << a->place << " has b.param = " << a->b->param;
for(size_t i=0;i<a->c.size();++i)
      cout << " c[" << i << "] = " << a->c[i]->xmltext;
delete a;

you get the idea, bindings.  Does such a thing exist outside of my ratty
little script?  I had a look at gdome but it doesn't appear to do what I want.  
I get the impression that Quick and Zeus could be the right thing, but this is
not for a Java project.

Is it possible to feed a DTD to XSLT and use it to generate C++ instead of

William Brodie-Tyrrell

Moderation in all things, particularly moderation.

<wfbrodie smug adelaide edu au>

xml mailing list, project page
xml gnome org
David Malcolm

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]