[xsd-users] Creating an XML without parsing an existing one

Yacoub, Sherif yacoub at amazon.com
Fri Jan 6 15:47:52 EST 2006


It worked fine. Thanks! 
I think we should try and post more examples as part of the distribution package.
I will try to forward some code snippit when I'm done.  

Thanks
-Sherif


 

-----Original Message-----
From: Boris Kolpackov [mailto:boris at codesynthesis.com] 
Sent: Friday, January 06, 2006 1:41 AM
To: Yacoub, Sherif
Cc: xsd-users at codesynthesis.com
Subject: Re: [xsd-users] Creating an XML without parsing an existing one

Sherif,

Yacoub, Sherif <yacoub at amazon.com> writes:

> However, I have a question about the creation of an XML from scratch 
> i.e. I don't have an XML and I want my program to create it.
>
> Using the library example in the distribution, what will be the 
> constructor to use to create an instance of the "catalog" root node 
> for example?

Citing Section 2.7, "Mapping for Complex Types" from the manual:

http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#2.7

"Additionally, the resulting C++ class defines a public constructor  that takes an initializer for each member of the complex type and  all its base types that belongs to the One cardinality class."

Looking at the catalog type definition

<xsd:complexType name="catalog">
  <xsd:sequence>
    <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

we conclude that the constructor won't have any arguments since the only member of this type (element book) belongs to the Sequence cardinality class (maxOccurs="unbounded"). Here is a code snippet that constructs a simple catalog from scratch:

using namespace library;

class catalog cat;

{
  book b (201700735,                              // ISBN
          title ("The C++ Programming Language"), // Title
          genre::philosophy,                      // Genre
          "CXXPL");                               // ID

  b.author ().push_back (author ("Bjarne Stroustrup", "1950-03-31"));

  cat.book ().push_back (b);
}

{
  book b (20170434,                       // ISBN
          title ("More Exceptional C++"), // Title
          genre::fiction,                 // Genre
          "MECXX");                       // ID

  author a ("Herb Sutter", "1960-03-31");
  a.recommends ("CXXPL");

  b.author ().push_back (a);
  cat.book ().push_back (b);
}

// The cat object now contains two books.

hth,
-boris




More information about the xsd-users mailing list