[xsd-users] issue creating document in memory

Boris Kolpackov boris at codesynthesis.com
Mon Jan 8 15:51:55 EST 2007


Hi Russ,

Russ Johns <johns at lanl.gov> writes:

> After getting a empty root element,  I get a access violation when I try to
> access the first sub element (minOccurs=maxOccurs=1)
> The cxx-tree classes were created by xsd with the --generate-serialization
> and --generate-default-ctor options.

Here is a note from the XSD Command Line Interface Doc[1] for the
--generate-default-ctor option:

"Generate default constructors even for types that have required
 members. Required members of an instance constructed using such
 a constructor are not initialized and accessing them results in
 undefined behavior."

That's exactly what happens to you. The reason why we do not
generate default constructors by default (no pun intended) is
to force you to initialize the required members. If you choose
to use the default c-tor then you will need to initialize
required members before you can access them or serialize the
in-memory representation to XML. In your case you can do something
like this:

si->elementlist (SWORD::input::ElementList ()) // initialize element list

SWORD::input::ElementList& el (si->elementlist()); // get element list ref

Note also that I am using reference (ElementList&) instead of just
ElementList; this way you get a reference to the ElementList that
is inside si (so that you can populate it).

You can also use a more efficient way which avoids copying (not really
important in your case but can be for large sub-trees):

auto_ptr<SWORD::input::ElementList> el (
  new SWORD::input::ElementList (si.get ()));

// populate el here

si->elementlist (el); // transfer el to si




[1] http://www.codesynthesis.com/projects/xsd/documentation/xsd.xhtml


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070108/f6b72ec1/attachment.pgp


More information about the xsd-users mailing list