Skip to main content

Developing with XCC

Coding Basics

To use XCC, there are several basic things you need to do in your Java code:

  1. Import the needed libraries.

  2. Set up the ContentSource object to authenticate against MarkLogic Server.

  3. Create a new Session object.

  4. Add a Request to the session object.

  5. Submit the request and get back a ResultSequence object from MarkLogic Server.

  6. Do something with the results (print them out, for example).

  7. Close the session.

The following are Java code samples that illustrate these basic design patterns:

package com.marklogic.xcc.examples;
import com.marklogic.xcc.ContentSource;
import com.marklogic.xcc.ContentSourceFactory;
import com.marklogic.xcc.Session;
import com.marklogic.xcc.Request;
import com.marklogic.xcc.ResultSequence;
URI uri = new URI("xcc://user:pass@localhost:8000/mycontent");
ContentSource contentSource = 
          ContentSourceFactory.newContentSource (uri);
Session session = contentSource.newSession();
Request request = session.newAdhocQuery ("\"Hello World\"");
ResultSequence rs = session.submitRequest (request);
System.out.println (rs.asString());
session.close();

Note

Session objects are not thread safe. A Session object should not be used concurrently by multiple threads.