com.marklogic.xcc.examples

## Class SimpleQueryRunner

- java.lang.Object
- - com.marklogic.xcc.examples.SimpleQueryRunner

-     public class SimpleQueryRunner
      extends java.lang.Object

  This is a very simple class that will submit an XQuery string to the server and return the result.

  Click here for the source code for this class

  The main() method looks for two command-line args, a URL for the server ( `ContentSourceFactory.newContentSource(java.net.URI)`) and a filename to read a query from. It loads the file, submits its contents to the server for evaluation and then prints the result sequence to stdout, one item per line.

  The class has methods the could be used to set request options and to obtain the results as an array of Strings or as a real `ResultSequence`.

  If you want to set external variables for a request, you can call `getRequest()` to obtain a reference to the internal `Request` object and set the variable values on it before invoking `execute(String)`.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SimpleQueryRunner(java.net.URI serverUri)` Construct an instance that will submit query requests to the server represented by the given URI.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ResultSequence` | `execute(java.lang.String query)` Submit the given query string and return a `ResultSequence` object.
    `java.lang.String` | `executeToSingleString(java.lang.String query, java.lang.String separator)` Submit the given query string and return a single String which is the concatenation of all the `ResultItem`s, separated by the given separator string.
    `java.lang.String[]` | `executeToStringArray(java.lang.String query)` Submit the given query string and return an array of Strings, possibly of length zero, which contains the String value of each `ResultItem` (see `XdmValue.asString()`)
    `Request` | `getRequest()` Returns the `Request` object used internally to submit requests.
    `static void` | `main(java.lang.String[] args)` Simple command-line driver that will read a query from a file and submit it to the server at the given URL.
    `void` | `setRequestOptions(RequestOptions options)` Set (or clear) the `RequestOptions` instance to associate with submitted queries.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleQueryRunner

          public SimpleQueryRunner(java.net.URI serverUri)
                            throws XccConfigException

      Construct an instance that will submit query requests to the server represented by the given URI. Note that the URI will not be validated at this time.
      Parameters:
        `serverUri` - A URI that specifies a server per ( `ContentSourceFactory.newContentSource(java.net.URI)`).
      Throws:
        `XccConfigException` - If the URI is not a valid XCC server URL.

  - ### Method Detail

    - #### execute

          public ResultSequence execute(java.lang.String query)
                                 throws RequestException

      Submit the given query string and return a `ResultSequence` object.
      Parameters:
        `query` - XQuery code as a String, to be evaluated by the server.
      Returns:
        An instance `ResultSequence`, possibly with size zero.
      Throws:
        `RequestException` - If an unrecoverable error occurs when submitting or evaluating the request.

    - #### executeToStringArray

          public java.lang.String[] executeToStringArray(java.lang.String query)
                                                  throws RequestException

      Submit the given query string and return an array of Strings, possibly of length zero, which contains the String value of each `ResultItem` (see `XdmValue.asString()`)
      Parameters:
        `query` - XQuery code as a String, to be evaluated by the server.
      Returns:
        An array of Strings, one per item in the `ResultSequence`.
      Throws:
        `RequestException` - If an unrecoverable error occurs when submitting or evaluating the request.

    - #### executeToSingleString

          public java.lang.String executeToSingleString(java.lang.String query,
                                                        java.lang.String separator)
                                                 throws RequestException

      Submit the given query string and return a single String which is the concatenation of all the `ResultItem`s, separated by the given separator string.
      Parameters:
        `query` - XQuery code as a String, to be evaluated by the server.
        `separator` - A String value which will be inserted in the final string between each item of the sequence. A value of null is equivalent to the empty string.
      Returns:
        A String consisting of the `XdmValue.asString()` value of each item with the separator string inserted between each instance.
      Throws:
        `RequestException` - If an unrecoverable error occurs when submitting or evaluating the request.

    - #### getRequest

          public Request getRequest()

      Returns the `Request` object used internally to submit requests. This object can be used to set external variables that will be bound to the query when submitted. You should not set your own `RequestOptions` object, use `setRequestOptions(com.marklogic.xcc.RequestOptions)` instead.
      Returns:
        An instance of `Request`.

    - #### setRequestOptions

          public void setRequestOptions(RequestOptions options)

      Set (or clear) the `RequestOptions` instance to associate with submitted queries.
      Parameters:
        `options` - An instance of `RequestOptions` or null.

    - #### main

          public static void main(java.lang.String[] args)
                           throws java.lang.Exception

      Simple command-line driver that will read a query from a file and submit it to the server at the given URL. No validation of the query file is done to insure that it really is XQuery or that it's a reasonable size.
      Parameters:
        `args` - First arg: a URL as per `ContentSourceFactory.newContentSource(java.net.URI)`. Second arg: a filename to read XQuery text from.
      Throws:
        `java.lang.Exception` - No exceptions are handled, anything that goes wrong will spew out a stack trace and exit.

Copyright © 2025 MarkLogic Corporation

Complete online documentation for MarkLogic Server, XQuery and related components may be found at docs.progress.com