Returning Results with Java XML APIs


Often, applications need to retrieve XQuery results as DOM, SAX, or StAX. XQSequence, as shown previously in this tutorial, allows access to the result as a direct mapping of the XQuery sequence. Within an XQSequence, XQItem objects represent each item in an XQuery sequence.

NOTE: Instantiating each item in an XQItem object affects performance because it requires the processing to create multiple objects. Use XQItem object judiciously.

Next, we’ll show you how to process an XQuery sequence and return the output as DOM, SAX, or StAX.

First, create an XQExpression object that executes the XQuery expression and returns a sequence of results:

DDXQDataSource ds = new DDXQDataSource(); 
... 
XQConnection conn = ds.getConnection("myuserid", "mypswd"); 
FileReader fileReader = new FileReader("flwor.xq"); 
XQExpression xqExpression = conn.createExpression(); 
XQSequence xqSequence = xqExpression.executeQuery(fileReader); 

DOM

To return the output from a result sequence as a DOM tree, we iterate over each DOM node in the XQuery sequence to extract the DOM content and print the DOM node to the standard System.out. For example, if you have J2SE 1.4.x, use the following code, which assumes all items in the result sequence are node items:

while(xqSequence.next()){ 
   Node domNode = xqSequence.getNode(); 
   System.out.println(domNode); 
   } 

If you have J2SE 1.5 and higher, the method is different; it is shown in the ResultRetrieval example.

SAX

To return the output from a result sequence as a SAX event stream rather than a string, create a SAX event handler (named SimpleSAXEventHandler, in this case) that sends the results to the standard System.out as shown in the following code:

SimpleSAXEventHandler anEventHandler = new SimpleSAXEventHandler(System.out); 
xqSequence.writeSequenceToSAX(anEventHandler); 

The complete application can be found in the ResultRetrieval example.

StAX

To return the output from a result sequence as a StAX event stream rather than as a string, create a StAX reader as shown in the following code:

XMLStreamReader reader = xqSequence.getSequenceAsStream(); 

You can use this StAX reader functionality like any other StAX stream reader. For example, the following code reads one event at a time and prints the event type together with the associated event names.

private static void formatOutput(XMLStreamReader reader) throws 
XMLStreamException { 
while(true){ 
    int event = reader.next(); 
    if(event == XMLStreamConstants.END_DOCUMENT){ 
    return;     
    switch (event) { 
      case XMLStreamConstants.START_ELEMENT: 
      System.out.println("Start tag: "); 
      printNames(reader); 
      break; 
       
      case XMLStreamConstants.END_ELEMENT: 
      System.out.println("End tag"); 
      printNames(reader); 
      break; 
       
      case XMLStreamConstants.CHARACTERS: 
      System.out.println("Text"); 
      printChars(reader); 
      break;      
   } 
} 
... 

Other similar examples can be found in the ResultRetrieval example.