Processing Results

We described in the previous page how to convert a complete Response document into a string. In this page we will focus on retrieving parts of the Response documents and converting these to other formats.

The Response document is composed of parts, or activity reports, which correspond to the activities in the activities. Each activity report contains the status and, if applicable, the result data.

Obtaining Activity Output from an SQL Query

There are basically two ways of formatting the output of an sqlQueryStatement activity: You can retrieve the data as a string (which is encoded as a XML document conforming to the WebRowSet schema) or as a JDBC ResultSet.

  1. Let's assume you have performed an SQL query across a database of your choice:
    SQLQuery query =
      new SQLQuery("select * from littleblackbook where id='3475'");
    service.perform( query );
    
  2. To retrieve the output you have to go back to your Activity object and obtain an ActivityOutput object:
    ActivityOutput output = query.getOutput();
    
  3. Now you can retrieve the data from the output, for example as a string:
    String result = output.getData();
    
  4. Or you can retrieve the output as a JDBC ResultSet object:
    ResultSet rs = query.getResultSet();
    
    This allows for further processing of the results using JDBC. For example, print out the result as a table, convert it to HTML, or feed it back into another database. We will see how this is done in later pages. By default, the ResultSet object will be of type ResultSet.TYPE_FORWARD_ONLY. If you would like more flexible access to the results, for example for use in an interactive application, this can be achieved by using the setResultSetType(int) method on the query object before calling getResultSet(). Warning: setting the result set type to ResultSet.TYPE_SCROLL_INSENSITIVE or ResultSet.TYPE_SCROLL_SENSITIVE causes the entire result set to be held in memory, so should be used with caution when dealing with very large result sets.

See examples/tutorials/clienttoolkit/ProcessingSQLQueryResults.java for an example solution.

Also see a later page on Pulling Data from a GDS back to the Client and examples/tutorials/clienttoolkit/ProcessingSQLQueryLargeResults.java for an example solution.

Obtaining Activity Output from an XPath Query

In this section you will learn how to retrieve output from an XPath query (xPathStatement) activity.

  1. Perform an XPath query:
    XPathStatement query = new XPathStatement("/entry[@id<10]");
    service.perform( query );
    
  2. Now you can retrieve the result data as a string:
    ActivityOutput output = query.getOutput();
    String data = output.getData();
    
  3. ... Or as a XMLDB ResourceSet object. The ResourceSet object contains a set of XML resources which can be retrieved as strings, along with some metadata. The metadata describes the XML resource to which the document fragment belongs.
    ResourceSet results = query.getResourceSet();
    ResourceIterator iter = results.getIterator();
    while(iter.hasMoreResources()) {
       Resource resource = iter.nextResource();
       String resourceID = resource.getId();
       String contents = resource.getContent());
    }
    
  4. By converting the Resource object into an ResourceImpl object (in package uk.org.ogsadai.client.toolkit.activity.xmldb), you can also retrieve the content as a W3C DOM node.
    Node node = ((ResourceImpl)resource).getContentAsDOM();
    

See examples/tutorials/clienttoolkit/ProcessingXPathQueryResults.java for an example solution.