Saving and Loading XML Documents

In this sample, two functions are presented. The first, based on the previous sample, saves a document to the archive. The second retrieves the document and prints it again. The direct streaming method cannot be used to create the document if it is to be saved.

Figure 1. Saving and Loading an XML Document
import curam.util.administration.struct.XSLTemplateInstanceKey;
import curam.util.xml.impl.XMLDocument;
import curam.util.xml.impl.XMLEncodingConstants;
import curam.util.xml.impl.XMLPrintStream;
import curam.util.xml.struct.XMLArchiveDocumentID;
import curam.util.xml.struct.XMLArchiveDocDetails;

public class XMLSample {

  /*
   * Creates an XMLDocument and saves it to the database.
   */
  XMLArchiveDocumentID saveDoc(
    XSLTemplateInstanceKey tempKey, MyStruct myStruct) {

    XMLDocument myDoc = new XMLDocument(
      XMLEncodingConstants.kEncodeISOLATIN1);

    myDoc.open("A User", "31-Dec-1999", "1.0", "Sample 1");
    myDoc.add(myStruct);
    myDoc.close();

    // Save the document to the database.
    final XMLArchiveDocumentID docKey =
      myDoc.save("Sample Saved Document 1", tempKey);
    return docKey;
  }


  /*
   * Loads an XMLDocument from the database and prints it.
   */
  void loadDoc(XMLArchiveDocumentID docKey) {

    // First load the archived data for the document and get
    // its template details and data content.
    final XMLDocument docForLoading = new XMLDocument(
      XMLEncodingConstants.kEncodeISOLATIN1);
    final XMLArchiveDocDetails docDetails =
      docForLoading.load(docKey);

    final XSLTemplateInstanceKey tempKey =
      new XSLTemplateInstanceKey();
    tempKey.templateID      = docDetails.templateID;
    tempKey.templateVersion = docDetails.templateVersion;
    tempKey.locale          = docDetails.locale;

    final String xmlContent = docDetails.document;

    docForLoading.close();

    // Now use this information to reconstruct a new
    // XMLDocument and print it.
    final XMLPrintStream myPrintStream =
      new XMLPrintStream();
    myPrintStream.open(tempKey, MyPC, 1234);
    myPrintStream.setEncoding(
      XMLEncodingConstants.kEncodeISOLATIN1);
    XMLDocument docForPrinting = new XMLDocument(
      myPrintStream.getStream(),
      XMLEncodingConstants.kEncodeISOLATIN1);
    docForPrinting.addFromXML(xmlContent);
    myPrintStream.close();
  }

}