XML Data Binding
This 60 second overview shows you the basic concept of generating source code from an XML Schema using Liquid XML Data Binder.
What is XML Data Binding?
XML Data Binding enables you to load XML Documents into a strongly typed object model within your source code.
Meaning fewer coding errors, reduced development and testing time, increased conformance and coding reliability.
Liquid XML Data Binder Features
- Generates an easy to use class library for C++.
- Generated HTML documentation for your class library API.
- Supports Smart Device platforms Android and iOS.
- Supports W3C XML Schema XSD, XDR and DTD standards.
- Supports JSON serialization.
- Supports Fast Infoset binary XML serialization.
- Support for the most complex XML standards.
- Royalty free distribution of compiled code and runtime.
XML Code Generator for C++
Platform Support
The C++ code is platform independent and has successfully been used on Windows, Linux, Solaris, HP-UX, MacOS, embedded systems and other more obscure platforms.
The full C++ runtime source code is available and is easily compiled onto other platforms (e.g. HP-UX, Mac OS, embedded systems).
Memory and Object Management
Memory Management is performed via reference counting, so no memory leaks. Smart Pointer classes are employed meaning you don't have to deal with any of the referencing counting. References are automatically released when your smart pointer goes out of scope.
Ease of Use
Strongly Typed Code
Liquid XML Data Binder creates simple intuitive code from your XML Schema. The generated code library contains strongly typed classes, collections, and enumerations to create an intuitive custom API to code against. Allowing you to concentrate on writing your Business logic code.
Collections
Collections are strongly typed and use the stl iterator pattern.
Properties
The generated classes expose the values in the XML as simple properties (getters and setters) allowing values to be easily manipulated.
Customisable Code
The generated output may also be modified within special 'Hand Coded Blocks', changes inside these blocks are persisted when the library is re-generated, allowing the library to be customised to fit the projects needs.
A Simple XML Data Binding Example for C++
The following C++ example we will use the Person.xsd, shown below to demonstrate how to read and write an XML document based on this schema.
<?xml version="1.0" encoding="utf-8" ?> <!--Created with Liquid Studio --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="Name" type="xs:string" /> <xs:element name="DateOfBirth" type="xs:date" /> <xs:element minOccurs="0" name="Address"> <xs:complexType> <xs:sequence> <xs:element name="HouseNo" type="xs:int" /> <xs:element name="PostCode" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element minOccurs="0" maxOccurs="unbounded" name="Car"> <xs:complexType> <xs:sequence> <xs:element name="Make" type="xs:string" /> <xs:element name="Model" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
<?xml version="1.0" encoding="utf-8" ?> <!--Created with Liquid Studio --> <Person> <Name>Fred</Name> <DateOfBirth>1978-06-26</DateOfBirth> <Address> <HouseNo>7</HouseNo> <PostCode>WV6 6JY</PostCode> </Address> <Car> <Make>Escort</Make> <Model>Ford</Model> </Car> <Car> <Make>Elise</Make> <Model>Lotus</Model> </Car> </Person>
Example C++ code to create the PersonSample.xml document using the strongly typed XML Data Binding generated code:
// create the root item 'person' CPersonPtr spPerson = CPerson::CreateInstance(); spPerson->SetName(_T("Fred")); spPerson->SetDateOfBirth(CDateTime(1978, 6, 26)); // add 'address' CAddressPtr spAddress = CAddress::CreateInstance(); spPerson->SetAddress(spAddress); spPerson->GetAddress()->SetHouseNo(7); spPerson->GetAddress()->SetPostCode(_T("WV6 6JY")); // add 'car1' cars to collection CCarPtr spCar1 = CCar::CreateInstance(); spPerson->GetCars()->Add(spCar1); spCar1->SetMake(_T("Ford")); spCar1->SetModel(_T("Escort")); // add 'car2' cars to collection CCarPtr spCar2 = CCar::CreateInstance(); spPerson->GetCars()->Add(spCar2); spCar2->SetMake(_T("Lotus")); spCar2->SetModel(_T("Elise")); // write xml document to string printf(_T("XML = %s"), spPerson->ToXml().c_str()); // write xml document to file spPerson->ToXmlFile(_T("SampleFile.xml"));
The XML Data Binding code is much easier to read and more concise than equivalent code written using generic DOM technology.
The strongly typed API allows errors to be detected at compile time should the data model change as opposed to DOM code where any errors will only be picked up in testing.
Example C++ code to read the PersonSample.xml document using XML Data Binding:
// create the root item 'person' CPersonPtr spPerson = CPerson::CreateInstance(); // load the items into the model spPerson->FromXmlFile(_T("SampleFile.xml")); // read items from the model printf(_T("%s was born %s"), spPerson->GetName().c_str(), spPerson->GetDateOfBirth().ToString().c_str()); printf(_T(", and lives at %d, %s\n"), spPerson->GetAddress()->GetHouseNo(), spPerson->GetAddress()->GetPostCode().c_str()); printf(_T("Cars Owned (%d)\n"), spPerson->GetCars()->GetCount()); for (CCarCol::iterator itr = spPerson->GetCars()->begin(); itr != spPerson->GetCars()->end(); itr++) { CCarPtr spCar = *itr; printf(_T(" %s, %s\n"), spCar->GetMake().c_str(), spCar->GetModel().c_str()); }
The XML Data Binding code provides the majority of the parsing for you, so manipulating the XML objects is a simple task of dealing with collections of strongly typed objects.
This is much easier than using generic DOM code, where you need to continually check the data type of an item.
In Summary
Easier to Code
XML Data Binding makes it significantly easier to deal with XML documents from within your C++ code, resulting in less code, which is simpler to read and maintain.
Easier to Test
As the generated class library is strongly typed, it forms a template for the developer, ensuring that the data created is valid and conforms to the underlying XML Schema.
Easier to Maintain
The maintenance phase of a project also befits as XML Data Binding allows any errors introduced because of changes in the data model to be caught early at compile time, unlike weakly typed DOM trees, which will give no indication of a problem until runtime. These kinds of changes can be a major cause of bugs which can only be caught in testing. Being able to identify these at compile time can save a significant amount of time and effort.