Encoding with Java XSD Code

Previous Menu Next

Encoding with Java Code Generated from XML Schemas

Encoding is similar to decoding. Let's look at how you would do the encoding to create the instance that we looked at earlier:

   <purchase>
     <customer number="12345">
       John Smith
     </customer>
     <store>
       Objective Systems Online Store
     </store>
     <item>
       XBinder XML Data Binding Tool
     </item>
     <price>
       750.00
     </price>
   </purchase>
   
		

You first have to import the definition for the FileOutputStream class:

   import java.io.FileOutputStream;

Then you need to import the definitions for the generated classes that you'll be using:

   import <package>.CustomerType;
   import <package>.PurchaseRecord;
   import <package>.PurchaseRecord_3;
   import <package>.Purchase_CC;

In the above "import" statements, <package> refers to the package into which the classes were generated.

And last, you need to import the definition for the XBXmlEncoder class from the XBinder Java runtime:

   import com.objsys.xbinder.runtime.XBXmlEncoder;

Then you need to create an instance of the generated Purchase_CC control class:

   Purchase_CC root = new Purchase_CC();

Your next step is to populate the generated structures with the information you want to appear in the instance. You start by creating an instance of the PurchaseRecord class, which is the class for the global element in the .xsd file:

   PurchaseRecord purchase = new PurchaseRecord();

Be careful not to get this confused with the control class. The control class is a class that encapsulates even this global element and gives you the ability to decode and encode the entire message.

At this point you can populate the objects with the data that needs to appear in the instance. Since the first item in the instance is a customer, you can set up the customer information first:

   CustomerType customer = new CustomerType();
   customer.setNumber(12345);
   customer.setValue("John Smith");
   purchase.setCustomer(customer);

This sets up a customer object that's referenced by the PurchaseRecord object.

Next you can define the value for the store element within the purchase record:

   purchase.setStore("Objective Systems Online Store");

Next is the possibly repeating sequence of item and price elements. To define one instance of this possibly repeating data, you need to create a PurchaseRecord_3 object:

   PurchaseRecord_3 itemAndPrice1 = new PurchaseRecord_3();

This object, then, can be populated with an item description and a price:

   itemAndPrice1.setItem("XBinder XML Data Binding Tool");
   itemAndPrice1.setPrice((float) 750.00);

Note the need to cast the literal 750.00 to (float). In Java decimal literals are double by default.

The next step is to put the populated item and price structure into the linked list that's generated to handle the sequence that might repeat:

   purchase.getPurchaseRecord_3().add(itemAndPrice1);

Suppose this customer also bought a bicycle for $100.00 in this same transaction. You could easily add that information into the objects:

   PurchaseRecord_3 itemAndPrice2 = new PurchaseRecord_3();
   itemAndPrice2.setItem("Bicycle");
   itemAndPrice2.setPrice((float) 100.00);
   purchase.getPurchaseRecord_3().add(itemAndPrice2);

The last step before encoding is to set the PurchaseRecord instance into the Purchase_CC control class:

   root.setPurchase(purchase);

And now the actual encoding can be done:

   String outputFile = "message_out.xml";
   FileOutputStream fout = new FileOutputStream(outputFile);
   XBXmlEncoder encoder;
   encoder = new XBXmlEncoder(fout, "UTF-8");
   root.encodeDocument(encoder);
   encoder.close();

The encodeDocument() method is a generated method to handle the encoding of a PurchaseRecord instance that's wrapped in a Purchase_CC control class instance.

The steps above write the encoded instance to a file. If desired, you could create the XBXmlEncoder object using a ByteArrayOutputStream instead of a FileOutputStream. This process would give you an in-memory copy of the encoded message.

So putting all of this together, you have something like this:

   import java.io.FileOutputStream;
   import <package>.CustomerType;
   import <package>.PurchaseRecord;
   import <package>.PurchaseRecord_3;
   import <package>.Purchase_CC;
   import com.objsys.xbinder.runtime.XBXmlEncoder;
   .
   .
   .
   Purchase_CC root = new Purchase_CC();

   PurchaseRecord purchase = new PurchaseRecord();

   CustomerType customer = new CustomerType();
   customer.setNumber(12345);
   customer.setValue("John Smith");

   purchase.setCustomer(customer);
   purchase.setStore("Objective Systems Online Store");

   PurchaseRecord_3 itemAndPrice1 = new PurchaseRecord_3();
   itemAndPrice1.setItem("XBinder XML Data Binding Tool");
   itemAndPrice1.setPrice((float) 750.00);

   purchase.getPurchaseRecord_3().add(itemAndPrice1);

   root.setPurchase(purchase);

   String outputFile = "message_out.xml";
   FileOutputStream fout = new FileOutputStream(outputFile);
   XBXmlEncoder encoder;
   encoder = new XBXmlEncoder(fout, "UTF-8");
   root.encodeDocument(encoder);
   encoder.close();
   .
   .
   .
   .

To compile this code, you need to specify the XBinder Java runtime jar as a dependency. This jar is named xbrt.jar and is included in your XBinder distribution in the "java" subdirectory underneath your main XBinder install directory. For example, if you installed XBinder into the folder c:\xbv260 on a Windows system, then the xbrt.jar file would be in c:\xbv260\java.

Previous Menu Next