[xsd-users] Need help with creating Object Model from scratch
    Boris Kolpackov 
    boris at codesynthesis.com
       
    Tue Sep 23 17:02:51 EDT 2008
    
    
  
Hi Laura,
Laura E Fowler <Laura_E_Fowler at raytheon.com> writes:
> I have been trying to figure out how to create an object model from 
> scratch with our specific schema. I can't find an example that is similar 
> to what we need to do. 
The easiest way is to start with the top level element. In your
case I believe it is Marker. This element is of anonymous type
so the name of its class will also be Marker. We start by 
creating an instance of Marker:
  Marker m (...);
Marker's constructor will have an argument for each required
element or attribute. In our case type Marker has four required
elements: id, deviceType, changed, and DeviceDescription. The
first three arguments are easy:
  Marker m ("marker-1", DeviceType::TYPEA, true, ...);
The last argument is of type DeviceDescription which itself
contains elements. So we first need to create an object of
this type:
  DeviceDescription dd;
  
  Marker m ("marker-1", DeviceType::TYPEA, true, dd);
But here our dd instance does not have any elements inside. So
we need to set one:
  DeviceDescriptionTypeB ddtb ("device-type-b");
  DeviceDescription dd;
  dd.DeviceDescriptionTypeB (ddtb);
  Marker m ("marker-1", DeviceType::TYPEA, true, dd);
The idea here is to start with the outer type and create and set
inner objects as required. Here is the complete example (assumes
the schema is saved as test.xsd):
#include <iostream>
#include "test.hxx"
using namespace std;
using namespace library;
int main ()
{
  DeviceDescriptionTypeB ddtb ("device-type-b");
  DeviceDescription dd;
  dd.DeviceDescriptionTypeB (ddtb);
  Marker m ("marker-1", DeviceType::TYPEA, true, dd);
  // Print.
  //
  cerr << m << endl;
  // Write to XML.
  //
  xml_schema::NamespaceInfomap map;
  map["lib"].name = "http://www.codesynthesis.com/library";
  map["lib"].schema = "test.xsd";
  Marker_ (std::cout, m, map);
}
Boris
    
    
More information about the xsd-users
mailing list