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*)>
<!ATTLIST a
      name CDATA #REQUIRED
      place CDATA #REQUIRED
      >
<!ELEMENT b EMPTY>
<!ATTLIST b
      param (false|true) "false">
<!ELEMENT c (#PCDATA)>

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

class xml_mydtd_a {
public:
      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 {
public:
      xml_mydtd_b(xmlDocPtr, xmlNodePtr);
      virtual ~xml_mydtd_b();

      enum param_ENUM {
              param_false,
              param_true
      } 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
HTML?


William Brodie-Tyrrell

--
Moderation in all things, particularly moderation.

<wfbrodie smug adelaide edu au>
http://www.cs.adelaide.edu.au/~william





_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml
-- 
David Malcolm
www.conglomerate.org




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