4.4.5.2 simpxml.java Client Code

The code in the following listing illustrates how Jolt works with a service with an XML type buffer. Because Jolt does not look into the XML data stream, it is the programmer's responsibility to ensure that the data formats between the Jolt client and the XML service match. The example in the following listing assumes that a session object was already instantiated.

Listing XML Buffer Type Example

/* Copyright 2001 Oracle Systems, Inc. All Rights Reserved */
/*
 * This code fragment illustrates how Jolt works with a service whose buffer
 * type is XML.
 */
import java.io.*;
import java.lang.*;
import bea.jolt.*;

public class xmldoc {

       public static void main (String[] args) {
              JoltSessionAttributes     sattr;
              JoltSession               session;
              JoltRemoteService         echo_xml;
              String inString = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><ORDER><HEADER DATE=\"05/13/1999\"
ORDERNO=\"22345\"/><COMPANY>ACME</COMPANY><LINE><ITEM MODEL=\"Pabc\"
QUANTITY=\"5\">LAPTOP</ITEM></LINE><LINE><ITEM MODEL=\"P500\"
QUANTITY=\"15\">LAPTOP</ITEM></LINE></ORDER>";

              byte data[];        
              DataInputStream din;
              DataOutputStream dout;
              ByteArrayInputStream bin;
              ByteArrayOutputStream bout;

              byte odata[];
              String outString = null;
              String appAddress = null;

              //...Create Jolt Session
              try {
                    /*
                     * Use java.io.DataOutputStream to put data
                     * into a byte array
                     */
                    bout = new ByteArrayOutputStream(inString.length());    
                    dout = new DataOutputStream(bout);
                    dout.writeBytes(inString);
                                /*
                     * Copy the byte array into a new byte array "data".
                     * Then issue the Jolt remote service call.
*/  
                     data = bout.toByteArray();
              } catch (Exception e) {
                     System.out.println("toByteArray error");
                     return;
}
try {
        echo_xml = new JoltRemoteService("ECHO_XML", session);
        System.out.println("JoltRemoteService Created");
        echo_xml.setBytes("XML", data, data.length);
} catch (Exception e) {
        System.out.println("RemoteService call error" + e);
        return;
}
echo_xml.call(null);
System.out.println("Service Call Returned");
odata = echo_xml.getBytesDef("XML", null);

try {
        System.out.println("Return String is:" + new
String(odata));
             } catch (Exception e) {
                     System.err.println("getByteDef Error");
             }
       }
}
// end of class