C++ Sample : Simple Sequence
Schema Summary This sample shows an element containing a sequence of simple elements and attributes. Schema Details The Element DVD contains a sequence of simple elements (simple because they are represented by primitives). All of these elements are required, and they must appear in the correct order. In addition the DVD element contains 2 attributes Genre and Cost. Genre is optional, Cost is Mandatory. Because Genre is optional, and contains primitive data, an IsValidGenre method is added to the generated DVD object. This allows you to determine if Genre was set or not. The generated classes take car of outputting the child elements in the correct order. Sample Description The sample demonstrates the element with all of the optional elements present. Note Genre is an optional primitive, so we should check it using the IsValid flag before accessing it |
Sample XML File
Sample2.xml |
<?xml version="1.0" encoding="UTF-8"?> <DVD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..\Schema\SimpleSequence.xsd" Cost="12.99" Genre="Horror"> <Title>From Hell</Title> <Description>A film about Jack the Ripper</Description> <ReleaseDate>2001-08-15</ReleaseDate> </DVD> |
Read Sample | |
#include "..\..\..\Samples\SimpleSequence\Generated\CPP\SourceCodeCPP\SimpleSequenceLib.h" #include "..\..\..\Samples\SimpleSequence\Generated\CPP\SourceCodeCPP\SimpleSequenceLib\DVD.h" std::string FilePath = SAMPLE_PATH + "SimpleSequence\\Samples\\Sample2.xml"; SampleRead(FilePath); void SampleRead(std::string FilePath) { // Create DVD object SimpleSequenceLib::CDVDPtr dvd = SimpleSequenceLib::CDVD::CreateInstance(); // Load data into the object from a file dvd->FromXmlFile(FilePath.c_str()); // Now we can look at the data printf("DVD Title = %s\n", dvd->GetTitle().c_str()); printf("DVD Description = %s\n", dvd->GetDescription().c_str()); printf("DVD Release Date = %s\n", dvd->GetReleaseDate().ToString("s").c_str() ); printf("DVD Cost = %f\n", dvd->GetCost()); if (dvd->IsValidGenre()) printf("DVD Genre = %s\n", dvd->GetGenre().c_str()); }
|
Write Sample | |
#include "..\..\..\Samples\SimpleSequence\Generated\CPP\SourceCodeCPP\SimpleSequenceLib.h" #include "..\..\..\Samples\SimpleSequence\Generated\CPP\SourceCodeCPP\SimpleSequenceLib\DVD.h" SimpleSequenceLib::CDVDPtr dvd = SimpleSequenceLib::CDVD::CreateInstance(); // Now we can look at the data dvd->SetTitle("From Hell"); dvd->SetDescription("A film about Jack the Ripper"); dvd->GetReleaseDate().SetDate(2001,8,15); dvd->SetCost(12.99); dvd->SetGenre("Horror"); // Now we can look at the XML from this object printf(dvd->ToXml().c_str());
|
SimpleSequence.xsd |
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="DVD"> <xs:complexType> <xs:sequence> <xs:element name="Title" type="xs:string"/> <xs:element name="Description" type="xs:string"/> <xs:element name="ReleaseDate" type="xs:date"/> </xs:sequence> <xs:attribute name="Genre" type="xs:string" use="optional"/> <xs:attribute name="Cost" type="xs:double" use="required"/> </xs:complexType> </xs:element> </xs:schema> |
Schema Diagrams |
|
DVD.h |
/********************************************************************************************** * Copyright (c) 2001-2023 Liquid Technologies Limited. All rights reserved. * See www.liquid-technologies.com for product details. * * Please see products End User License Agreement for distribution permissions. * * WARNING: THIS FILE IS GENERATED * Changes made outside of ##HAND_CODED_BLOCK_START blocks will be overwritten * * Generation : by Liquid XML Data Binder 19.0.14.11049 * Using Schema: SimpleSequence.xsd **********************************************************************************************/ #ifndef _SimpleSequenceLib_SimpleSequenceLib_CDVD_h #define _SimpleSequenceLib_SimpleSequenceLib_CDVD_h // Include Base classes // ##HAND_CODED_BLOCK_START ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional Includes here... // ##HAND_CODED_BLOCK_END ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Forward declarations - done like this to keep the intellisense happy namespace SimpleSequenceLib { class CClassFactory; }; namespace SimpleSequenceLib { /* * CDVD * * This class wraps the element DVD in the schema */ class SimpleSequenceLib_DLL CDVD : public CInstanceMonitor , public virtual SimpleSequenceLib::CXmlCommonBase // ##HAND_CODED_BLOCK_START ID="Additional Base Classes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional base classes here... // ##HAND_CODED_BLOCK_END ID="Additional Base Classes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS { public: static SimpleSequenceLib::CDVDPtr CreateInstance(LPCTSTR lpctElementName=_T("DVD")); protected: CDVD(LPCTSTR lpctElementName=_T("DVD")); virtual ~CDVD(); friend class SimpleSequenceLib::CClassFactory; virtual void Init(); virtual void AccessProperty(int iPropertyIndex, bool read, LtXmlLib20::LtVariant& rValue); virtual LtXmlLib20Data::CParentElementInfo* GetClassInfo() const; virtual LtXmlLib20Data::CElementInfo** GetClassElementInfo() const; virtual LtXmlLib20Data::CAttributeInfo** GetClassAttributeInfo() const; static void CleanMetaData(); void Cleanup(); virtual void OnEvent(LtXmlLib20::CXmlObjectBase* pMsgSource, LtXmlLib20::IEventSink::MsgType eMsgType, void* pData); public: std::tstring GetGenre() const; void SetGenre(std::tstring val); bool IsValidGenre() const; void SetValidGenre(bool val); protected: bool m_IsValidGenre; std::tstring m_Genre; public: DOUBLE GetCost() const; void SetCost(DOUBLE val); protected: DOUBLE m_Cost; public: std::tstring GetTitle() const; void SetTitle(std::tstring val); protected: std::tstring m_Title; public: std::tstring GetDescription() const; void SetDescription(std::tstring val); protected: std::tstring m_Description; public: LtXmlLib20::CDateTime GetReleaseDate() const; void SetReleaseDate(LtXmlLib20::CDateTime val); protected: LtXmlLib20::CDateTime m_ReleaseDate; public: // Performs a 'deep copy' of all the data in the class (and its children) virtual SimpleSequenceLib::CDVDPtr Clone() const; virtual std::tstring GetTargetNamespace() const; virtual std::tstring GetNamespace() const; virtual LtXmlLib20::CXmlObjectBase* GetBase(); // Internal data for XML serialization private: static LtXmlLib20Data::CParentElementInfo* ms_pParentElementInfo; static LtXmlLib20Data::CElementInfo** ms_ppElementInfo; static LtXmlLib20Data::CAttributeInfo** ms_ppAttributeInfo; // ##HAND_CODED_BLOCK_START ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional Methods here... // ##HAND_CODED_BLOCK_END ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS }; }; // end namespace (SimpleSequenceLib) #endif // _SimpleSequenceLib_CDVD_h |
DVD.cpp |
/********************************************************************************************** * Copyright (c) 2001-2023 Liquid Technologies Limited. All rights reserved. * See www.liquid-technologies.com for product details. * * Please see products End User License Agreement for distribution permissions. * * WARNING: THIS FILE IS GENERATED * Changes made outside of ##HAND_CODED_BLOCK_START blocks will be overwritten * * Generation : by Liquid XML Data Binder 19.0.14.11049 * Using Schema: SimpleSequence.xsd **********************************************************************************************/ #include "StdAfx.h" #pragma warning (push) #pragma warning (disable:4251) // template export warning #pragma warning (disable:4786) // long debug names #include "../SimpleSequenceLib.h" #include "../SimpleSequenceLib/DVD.h" // ##HAND_CODED_BLOCK_START ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional Includes here... // ##HAND_CODED_BLOCK_END ID="Additional Includes"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Name : DVD // Long Name : DVD // Element Name : DVD // Class Namespace : SimpleSequenceLib // Namespace Alias : // Schema Namespace : // Mapped Class Name : CDVD // Mapped Class Full Name : SimpleSequenceLib::CDVD // Mapped Class File Name : CDVD // IsAbstract : False // IsElement : True // IsComplexType : False namespace SimpleSequenceLib { LtXmlLib20Data::CParentElementInfo* CDVD::ms_pParentElementInfo = NULL; LtXmlLib20Data::CAttributeInfo** CDVD::ms_ppAttributeInfo = NULL; LtXmlLib20Data::CElementInfo** CDVD::ms_ppElementInfo = NULL; CDVDPtr CDVD::CreateInstance(LPCTSTR lpctElementName/*=_T("DVD")*/) { return new SimpleSequenceLib::CDVD(lpctElementName); } /* * Constructor for CDVD * * The class is created with all the mandatory fields populated with the * default data. * All Collection objects are created. * However any 1-n relationships (these are represented as collections) are * empty. To comply with the schema these must be populated before the xml * obtained from ToXml is valid against the schema SimpleSequence.xsd */ CDVD::CDVD(LPCTSTR lpctElementName/*=_T("DVD")*/) : CInstanceMonitor(_T("CDVD")) , m_ReleaseDate(LtXmlLib20::CDateTime::dt_date) { m_elementName = lpctElementName; Init(); } CDVD::~CDVD() { Cleanup(); } void CDVD::Cleanup() { // unregister for any events we have asked for // cos there'll be no one left to hear soon } void CDVD::OnEvent(LtXmlLib20::CXmlObjectBase* pMsgSource, LtXmlLib20::IEventSink::MsgType eMsgType, void* pData) { if (eMsgType == LtXmlLib20::IEventSink::MT_CollectionChangeEvent) { } } /* * Initializes the class * * The Creates all the mandatory fields (populated with the default data) * All Collection object are created. * However any 1-n relationships (these are represented as collections) are * empty. To comply with the schema these must be populated before the xml * obtained from ToXml is valid against the schema SimpleSequence.xsd. */ void CDVD::Init() { Cleanup(); this->m_Genre = _T(""); this->m_IsValidGenre = false; this->m_Cost = 0; this->m_Title = _T(""); this->m_Description = _T(""); this->m_ReleaseDate = LtXmlLib20::CDateTime(LtXmlLib20::CDateTime::dt_date); // ##HAND_CODED_BLOCK_START ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional Init Settings... // ##HAND_CODED_BLOCK_END ID="Additional Inits"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS } void CDVD::AccessProperty(int iPropertyIndex, bool bRead, LtXmlLib20::LtVariant& rValue) { if (bRead) { switch(iPropertyIndex) { case 1: if (IsValidGenre()) rValue.SetString(GetGenre()); else rValue.SetInvalid(); break; case 2: rValue.SetR8(GetCost()); break; case 3: rValue.SetString(GetTitle()); break; case 4: rValue.SetString(GetDescription()); break; case 5: rValue.SetDate(GetReleaseDate()); break; default: throw LtXmlLib20::CLtException(_T("Unknown Property Index")); }; } else { switch(iPropertyIndex) { case 1: if (rValue.IsValid()) SetGenre(rValue.GetString()); else SetValidGenre(false); break; case 2: SetCost(rValue.GetR8()); break; case 3: SetTitle(rValue.GetString()); break; case 4: SetDescription(rValue.GetString()); break; case 5: SetReleaseDate(rValue.GetDate()); break; default: throw LtXmlLib20::CLtException(_T("Unknown Property Index")); } } } /* * Represents an optional Attribute in the XML document * * * This property is represented as an Attribute in the XML. * It is optional, initially it is not valid. */ std::tstring CDVD::GetGenre() const { if (m_IsValidGenre == false) throw LtXmlLib20::CLtInvalidStateException(_T("The Property GetGenre is not valid. SetGenre must be called first")); return this->m_Genre; } void CDVD::SetGenre(std::tstring value) { // Apply whitespace rules appropriately value = LtXmlLib20::CWhitespaceUtils::Preserve(value); this->m_IsValidGenre = true; this->m_Genre = value; } /* * Indicates if GetGenre contains a valid value. * * true if the value for GetGenre is valid, false if not. * If this is set to true then the property is considered valid, and assigned its * default value (_T("")). * If its set to false then its made invalid, and subsequent calls to GetGenre * will raise an exception. */ bool CDVD::IsValidGenre() const { return m_IsValidGenre; } void CDVD::SetValidGenre(bool value) { if (value != m_IsValidGenre) { this->m_Genre = _T(""); m_IsValidGenre = value; } } /* * Represents a mandatory Attribute in the XML document * * * This property is represented as an Attribute in the XML. * It is mandatory and therefore must be populated within the XML. * It is defaulted to 0. */ DOUBLE CDVD::GetCost() const { return this->m_Cost; } void CDVD::SetCost(DOUBLE value) { this->m_Cost = value; } /* * Represents a mandatory Element in the XML document * * * This property is represented as an Element in the XML. * It is mandatory and therefore must be populated within the XML. * It is defaulted to _T(""). */ std::tstring CDVD::GetTitle() const { return this->m_Title; } void CDVD::SetTitle(std::tstring value) { // Apply whitespace rules appropriately value = LtXmlLib20::CWhitespaceUtils::Preserve(value); this->m_Title = value; } /* * Represents a mandatory Element in the XML document * * * This property is represented as an Element in the XML. * It is mandatory and therefore must be populated within the XML. * It is defaulted to _T(""). */ std::tstring CDVD::GetDescription() const { return this->m_Description; } void CDVD::SetDescription(std::tstring value) { // Apply whitespace rules appropriately value = LtXmlLib20::CWhitespaceUtils::Preserve(value); this->m_Description = value; } /* * Represents a mandatory Element in the XML document * * * This property is represented as an Element in the XML. * It is mandatory and therefore must be populated within the XML. * It is defaulted to LtXmlLib20::CDateTime(LtXmlLib20::CDateTime::dt_date). */ LtXmlLib20::CDateTime CDVD::GetReleaseDate() const { return this->m_ReleaseDate; } void CDVD::SetReleaseDate(LtXmlLib20::CDateTime value) { this->m_ReleaseDate.SetDateTime(value, this->m_ReleaseDate.GetType()); } /* * Allows the class to be copied * Performs a 'deep copy' of all the data in the class (and its children) */ SimpleSequenceLib::CDVDPtr CDVD::Clone() const { SimpleSequenceLib::CDVDPtr newObject = CreateInstance(m_elementName.c_str()); int index = 0; newObject->m_Genre = m_Genre; newObject->m_IsValidGenre = m_IsValidGenre; newObject->m_Cost = m_Cost; newObject->m_Title = m_Title; newObject->m_Description = m_Description; newObject->m_ReleaseDate = m_ReleaseDate; // ##HAND_CODED_BLOCK_START ID="Additional clone"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional clone code here... // ##HAND_CODED_BLOCK_END ID="Additional clone"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS return newObject.Ptr(); } std::tstring CDVD::GetTargetNamespace() const { return _T(""); } std::tstring CDVD::GetNamespace() const { return _T(""); } LtXmlLib20::CXmlObjectBase* CDVD::GetBase() { return this; } void CDVD::CleanMetaData() { LtXmlLib20::CXmlGeneratedClass::CleanMetaData(ms_pParentElementInfo, ms_ppElementInfo, ms_ppAttributeInfo); } LtXmlLib20Data::CParentElementInfo* CDVD::GetClassInfo() const { if (ms_pParentElementInfo == NULL) { m_csInit.Enter(); if (ms_pParentElementInfo == NULL) { ms_pParentElementInfo = new LtXmlLib20Data::CParentElementInfo( LtXmlLib20Data::XmlElementGroupType_SEQUENCE, LtXmlLib20Data::XmlElementType_ELEMENT, _T("DVD"), _T(""), true, false, -1, LtXmlLib20::ItemType_none, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_None, NULL, false); } m_csInit.Leave(); } return ms_pParentElementInfo; } LtXmlLib20Data::CElementInfo** CDVD::GetClassElementInfo() const { if (ms_ppElementInfo == NULL) { m_csInit.Enter(); if (ms_ppElementInfo == NULL) { ms_ppElementInfo = new LtXmlLib20Data::CElementInfo*[4]; ms_ppElementInfo[0] = new LtXmlLib20Data::CElementInfoSeqPrimMnd(_T("Title"), _T(""), 3, false, LtXmlLib20::ItemType_string, NULL, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_Preserve, LtXmlLib20::CPrimitiveRestrictions(_T(""), -1, -1, _T(""), _T(""), _T(""), _T(""), -1, -1, -1), NULL); ms_ppElementInfo[1] = new LtXmlLib20Data::CElementInfoSeqPrimMnd(_T("Description"), _T(""), 4, false, LtXmlLib20::ItemType_string, NULL, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_Preserve, LtXmlLib20::CPrimitiveRestrictions(_T(""), -1, -1, _T(""), _T(""), _T(""), _T(""), -1, -1, -1), NULL); ms_ppElementInfo[2] = new LtXmlLib20Data::CElementInfoSeqPrimMnd(_T("ReleaseDate"), _T(""), 5, false, LtXmlLib20::ItemType_date, NULL, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_Collapse, LtXmlLib20::CPrimitiveRestrictions(_T(""), -1, -1, _T(""), _T(""), _T(""), _T(""), -1, -1, -1), NULL); ms_ppElementInfo[3] = NULL; } m_csInit.Leave(); } return ms_ppElementInfo; } LtXmlLib20Data::CAttributeInfo** CDVD::GetClassAttributeInfo() const { if (ms_ppAttributeInfo == NULL) { m_csInit.Enter(); if (ms_ppAttributeInfo == NULL) { ms_ppAttributeInfo = new LtXmlLib20Data::CAttributeInfo*[3]; ms_ppAttributeInfo[0] = new LtXmlLib20Data::CAttributeInfoPrimitive(_T("Genre"), _T(""), 1, true, LtXmlLib20::ItemType_string, NULL, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_Preserve, LtXmlLib20::CPrimitiveRestrictions(_T(""), -1, -1, _T(""), _T(""), _T(""), _T(""), -1, -1, -1), NULL); ms_ppAttributeInfo[1] = new LtXmlLib20Data::CAttributeInfoPrimitive(_T("Cost"), _T(""), 2, false, LtXmlLib20::ItemType_r8, NULL, LtXmlLib20::CWhitespaceUtils::WhitespaceRule_Collapse, LtXmlLib20::CPrimitiveRestrictions(_T(""), -1, -1, _T(""), _T(""), _T(""), _T(""), -1, -1, -1), NULL); ms_ppAttributeInfo[2] = NULL; } m_csInit.Leave(); } return ms_ppAttributeInfo; } // ##HAND_CODED_BLOCK_START ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS // Add Additional Methods here... // ##HAND_CODED_BLOCK_END ID="Additional Methods"## DO NOT MODIFY ANYTHING OUTSIDE OF THESE TAGS }; // namespace |
Main Menu | Samples List |