Decoding with Java XSD Code

Previous Menu Next

Decoding with Java 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>
   

First, there are several classes whose definitions you will have to import:

   import javax.xml.stream.XMLInputFactory;
   import javax.xml.stream.XMLStreamReader;
   import java.io.InputStream;
   import java.io.BufferedInputStream;
   import java.io.FileInputStream;
   import <package>.Purchase_CC;

In the last "import" statement, <package> refers to the name of the package into which the Purchase_CC class is generated.

Your first step is to initialize a stream object that maps to your input file (assumed to be the String variable "inputFile" below). This is accomplished by using the InputStream, BufferedInputStream, and FileInputStream classes.

   InputStream in = new BufferedInputStream(new FileInputStream(inputFile));

Your next step is to create an XMLStreamReader object against your input stream:

   XMLStreamReader reader = null;
   try  {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      reader = inputFactory.createXMLStreamReader( inputFile, in );

With that done, your next step is to create an instance of the control class object:

   Purchase_CC root = new Purchase_CC();

Now the decode operation can be done. This is accomplished by calling the generated decodeDocument() method on the Purchase_CC class:

   root.decodeDocument(reader);

So putting this all together, you'd have something like this:

   import javax.xml.stream.XMLInputFactory;
   import javax.xml.stream.XMLStreamReader;
   import java.io.InputStream;
   import java.io.BufferedInputStream;
   import java.io.FileInputStream;
   import <package>.Purchase_CC;
   .
   .
   .
   InputStream in = new BufferedInputStream(new FileInputStream(inputFile));
   XMLStreamReader reader = null;
   try  {
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      reader = inputFactory.createXMLStreamReader( inputFile, in );
      Purchase_CC root = new Purchase_CC();
      root.decodeDocument(reader);
      .
      .
      .
   }
   catch (Exception e) {
      .
      .
      .
   }

Previous Menu Next