Decoding with C++ Code Generated from XML Schemas
Let's first look at how you would decode an instance that conforms to the schema in the Purchase.xsd file that we've been using, which is reproduced here:
Your first step is to initialize a stream object that maps to your input file. This is accomplished by using the OSRTFileInputStream class, which is defined in the XBinder C++ run-time library. To use it you need to include the header file that defines it:
Then you can construct an instance mapped to your input file (assumed to be the char* variable "filename" here):
With that done, your next step is to create a decode buffer object. The definition for this object's class is in the following header file, which you must include:
Note that this header file also includes definitions for the encode buffer object, which is used in the encoding process.
The object can then be created:
Note that the decode buffer object maps to the input stream, which in turn maps to the file that holds the instance to be decoded.
In the remaining steps you will be making use of generated class definitions. These definitions are all defined within the following header file, which you must include:
You now need to create an instance of a generated class that corresponds to the global element defined in your .xsd file. This global element is sometimes referred to in the generated code as a PDU (Protocol Data Unit). This term is borrowed from ASN.1 and refers to the top-level element that a .xsd file defines; i.e., the element that contains all of the data.
In this case the name of the generated class is purchase_CC (CC for control class):
Note that the created instance is associated with the decode buffer object, which is associated with the input stream object, which is associated with the file containing the instance to decode.
With this done you're ready to do the actual decode. The decode is accomplished by calling the generated decode() method on the purchase_CC instance:
The value that the method returns is an int value. A value of 0 indicates success.
So putting this all together, you'd have something like this:
To compile this code, you need to specify the XBinder install directory as a directory to search for included .h files. For example, if you installed XBinder into the folder c:\xbv260 on a Windows system, then you would specify c:\xbv260 as a directory to search for included .h files during the compile.
To link the code you need to specify a couple of libraries whose specific names and locations will vary depending on what compiler you're using and what version of XBinder you have. For example, for XBinder v2.6 on Windows, the libraries in the cpp\lib hierarchy are built with Visual Studio 2015. In newer versions of XBinder these libraries might be built with a different tool, most likely a newer version of Visual Studio. See the table below for some examples for v2.6 on Windows:
|Microsoft Visual Studio 2015||cpp\lib\osysrt_a.lib
cpp\lib\osysrtxml.lib or osysrtxml_a.lib
|Microsoft Visual Studio 2017||cpp_vs2017\lib\osysrt_a.lib
cpp_vs2017\lib\osysrtxml.lib or osysrtxml_a.lib
In each case in the table above the indicated directory structure lives underneath the XBinder install directory.