[xsd-users] Example of Anonymous Types

Lester Memmott lmemmott at yahoo.com
Fri Jun 1 12:34:18 EDT 2007


All,

A sincere thanks for the response, Boris.  From the details you provided it looks like there is a lot of flexibility in the tool.  For the moment, I'd like to get a basic proof-of-concept working that shows that anonymous types can be used (so that I don't have to rewrite schema files) and still write reasonable C++ code to access the object bound to the XML document.

For those following this thread, I was able to work up an example using the same xml, xsd I started this thread with without rewriting the xsd from anonymous types to named types.

Case 1:
I created the .cxx and .hxx source in the following fashion:  (again .xsd was using anonymous types)
xsd cxx-tree --morph-anonymous AkodGuiConfig.xsd

And the corresponding code looks like:
void AkodGuiConfigTestMorphOn(char * argv)
{
    std::auto_ptr<AkodGuiConfig> h(AkodGuiConfig_(argv)); 
    //Get the PredefinedIdentifiers info within the AkodGuiConfig tag
    for (PredefinedIdentifiers::IdentifierDescription::iterator pii(h->PredefinedIdentifiers().IdentifierDescription().begin()); 
    pii != h->PredefinedIdentifiers().IdentifierDescription().end(); 
    ++pii)
    {
        //Get the XML attributes
        cout << "PredefinedIdentifiers Info: " << endl;
        cout << pii->Description() << endl;
        cout << pii->UnitOfMeasure() << endl;
        cout << pii->IntrinsicDataType() << endl;
        cout << pii->Multivalued() << endl;
        cout << pii->SupportsEvents() << endl;
        cout << pii->CanBeProjected() << endl;
        cout << pii->DefaultValue() << endl;
        //Get the XML Value (i.e. CDATA / content)
        cout << *pii << endl; 
        cout << endl;
    }
    cout << endl << endl;
    //Get the CurrentlyUsedIdentifiers info within the AkodGuiConfig tag
    for (CurrentlyUsedIdentifiers::Identifier::iterator cuii(h->CurrentlyUsedIdentifiers().Identifier().begin()); 
    cuii != h->CurrentlyUsedIdentifiers().Identifier().end(); 
    ++cuii)
    {
        //Get the XML attributes
        cout << "CurrentlyUsedIdentifiers Info: " << endl;
        cout << cuii->Name() << endl;
        if (cuii->LastKnownValue().present()) //check for existance since it is an optional parameter
            cout << cuii->LastKnownValue().get() << endl;
        cout << endl;
    }
}

Case 2:
I created the .cxx and .hxx source in the following fashion:
xsd cxx-tree AkodGuiConfig.xsd
Note:  Did not use --morph-anonymous

And the corresponding code looks like:
void AkodGuiConfigTestMorphOff(char * argv)
{
    std::auto_ptr<AkodGuiConfig::type> h(AkodGuiConfig(argv)); 
    
    //Get the PredefinedIdentifiers info within the AkodGuiConfig tag
    for (AkodGuiConfig::type::PredefinedIdentifiers::type::IdentifierDescription::iterator pii(h->PredefinedIdentifiers().IdentifierDescription().begin()); 
    pii != h->PredefinedIdentifiers().IdentifierDescription().end(); 
    ++pii)
    {
        //Get the XML attributes
        cout << "PredefinedIdentifiers Info: " << endl;
        cout << pii->Description() << endl;
        cout << pii->UnitOfMeasure() << endl;
        cout << pii->IntrinsicDataType() << endl;
        cout << pii->Multivalued() << endl;
        cout << pii->SupportsEvents() << endl;
        cout << pii->CanBeProjected() << endl;
        cout << pii->DefaultValue() << endl;
        //Get the XML Value (i.e. CDATA / content)
        cout << *pii << endl; 
        cout << endl;
    }
    cout << endl << endl;
    //Get the CurrentlyUsedIdentifiers info within the AkodGuiConfig tag
    for (AkodGuiConfig::type::CurrentlyUsedIdentifiers::type::Identifier::iterator cuii(h->CurrentlyUsedIdentifiers().Identifier().begin()); 
    cuii != h->CurrentlyUsedIdentifiers().Identifier().end(); 
    ++cuii)
    {
        //Get the XML attributes
        cout << "CurrentlyUsedIdentifiers Info: " << endl;
        cout << cuii->Name() << endl;
        if (cuii->LastKnownValue().present()) //check for existance since it is an optional parameter
            cout << cuii->LastKnownValue().get() << endl;
        cout << endl;
    }
}

So in summary, the bulk of the code is the same between between named-types vs anonymous types (morph on) and anonymous types (morph off).  The only part that gets tricky is knowing how to declare the objects and the iterators.  You'll note that in the case with morph off the iterator type name gets rather long.

Thanks,
Lester


More information about the xsd-users mailing list