Decoding with C++ XSD Code

Previous Menu Next

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:

   <xsd:element name="purchase" type="PurchaseRecord"/>

   <xsd:complexType name="PurchaseRecord">
      <xsd:sequence>
         <xsd:element name="customer" type="CustomerType" maxOccurs="1"/>
         <xsd:element name="store" type="xsd:string" maxOccurs="1"/>
         <xsd:sequence maxOccurs="unbounded">
            <xsd:element name="item" type="xsd:string"/>
            <xsd:element name="price" type="xsd:float"/>
         </xsd:sequence>
      </xsd:sequence>
   </xsd:complexType>

   <xsd:complexType name="CustomerType">
      <xsd:simpleContent>
         <xsd:extension base="xsd:string">
            <xsd:attribute name="number" type="xsd:integer"/>
         </xsd:extensiion>
      </xsd:simpleContent>
   </xsd:complexType>
   

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:

   #include "rtxsrc/OSRTFileInputStream.h"

Then you can construct an instance mapped to your input file (assumed to be the char* variable "filename" here):

   OSRTFileInputStream in (filename);

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:

   #include "rtxmlsrc/rtXmlCppMsgBuf.h"

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:

   OSXMLDecodeBuffer decodeBuffer (in);

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:

   #include "Purchase.h"

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):

   purchase_CC pdu (decodeBuffer);

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:

   stat = pdu.decode();

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:

   #include "rtxsrc/OSRTFileInputStream.h"
   #include "rtxmlsrc/rtXmlCppMsgBuf.h"
   #include "Purchase.h"
   .
   .
   .
   int stat;
   char* filename = "Purchase.xml";
   OSRTFileInputStream in (filename);
   OSXMLDecodeBuffer decodeBuffer (in);
   purchase_CC pdu (decodeBuffer);
   stat = pdu.decode();
   .
   .
   .
   .

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:

CompilerLibraries
Microsoft Visual Studio 2015cpp\lib\osysrt_a.lib
cpp\lib\osysrtxml.lib or osysrtxml_a.lib
Microsoft Visual Studio 2017cpp_vs2017\lib\osysrt_a.lib
cpp_vs2017\lib\osysrtxml.lib or osysrtxml_a.lib
gccc_gnu/libosysrt.a
c_gnu/libosysrtxml.a

 

In each case in the table above the indicated directory structure lives underneath the XBinder install directory.

Previous Menu Next