Adding documents to a document base

How to populate a document base (when there is no versioning).

Your program must be connected to a TEXTML Server instance.

Your program must be connected to a docbase on that server, and your program must have an IxiaDocBaseServices object for that docbase:
IxiaDocBaseServices docbase =   // ...

This section explains how to add documents to a document base with no versioning.

Versioning means that a docbase can store more than one version of a document: each version has the same document name, but a unique version number. No versioning means that a new version of a document replaces the existing document.

In the code snippets below, the documents to be added are files located in a path stored in String sourceDirectory.

To add documents to a document base:

  1. Get the DocumentServices object for the specified docbase:
    IxiaDocumentServices ds = docbase.DocumentServices();

    DocumentServices allow you get (retrieve) documents from a docbase, to set (add) documents, to copy documents from one collection to another, to lock documents, and to remove (delete) documents.

  2. Create a document list, i.e., an array of documents to be added to the docbase.
    Each document is an IxiaDocument object that stores the contents of one file in sourceDirectory. The code snippet below shows how to create a document list that contains one document for each file contained in sourceDirectory and in all of its subdirectories (if any).
    // Create an array of File objects, one for each file/directory
    // stored in <sourceDirectory>
    File[] files = sourceDirectory.listFiles();
    // Create an ArrayList structure with one element for
    // each file/directory
    ArrayList documents = new ArrayList(files.length);
    // For each file/directory...
    for (int i = 0; i < files.length; ++i)
        // If it is a directory, and user wants recursion, then recurse
        if (files[i].isDirectory())
            if (recurse)
                Process(files[i], ds, true);
            // It is a file. Create a Document object for it.
            IxiaDocument document = IxiaDocument.getInstance();
            // Let's assume that all files in the directory are
            // XML files.
            // Add the document to the ArrayList of documents
    // Create <docList>,
    // an array of documents of the same size as the ArrayList
    IxiaDocument[] docList = new IxiaDocument[documents.size()];
    // Fill the array with the contents of the ArrayList.
    // Note: toArray() is an overloaded generic method!
    docList = (IxiaDocument[]) documents.toArray(docList);
  3. Add the documents in the document list to docbase ds:
    // Add the documents to the docbase.
    // Index the documents.
    // If a document with the same name already exists in the docbase,
    // then replace the old document with the new one.
    // The documents are all of type "user document" (i.e., they are
    // not system documents).
    TextmlserverError [] err =
        ds.SetDocuments(docList, Constants.TEXTML_ADD_DOCUMENT |
                                 Constants.TEXTML_REPLACE_DOCUMENT |
  4. Handle any errors that may have occurred while adding the list of documents to the docbase.
    // Process the errors, if any
    int countError = 0;
    if (err != null)
        for (int i = 0; i < err.length; ++i)
            if (err[i] != null)
                if (countError == 1)
                    // getMessage() returns an XML string
                       ("Error = " + err[i].getMessage());
    System.err.println("End of adding the documents") ;
    System.err.println("Error count = " + countError);

    Adding a list of documents to a docbase may trigger an error for none, some, or all of the documents. You can handle these errors only after IxiaDocumentServices.SetDocuments() returns. You handle them by processing the array of IxiaTextmlServerError objects. The messages returned by the Error objects are in XML.

You can build and run sample program SetDocuments: