qexo-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qexo-general] Calling XQuery from Java


From: Phil Shaw
Subject: [Qexo-general] Calling XQuery from Java
Date: Sun, 19 Jan 2003 22:36:47 -0800 (PST)

 
 

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com


Hi,

I hope I'm now properly registered to post messages.

I'm trying to evaluate XQuery expressions in Java,
as described in the "Calling XQuery Expressions from Java"
of "Running.html".

First I tried the simple "RunXQuery" class, and I can't
get it to work at all, because the "eval(String)" method
returns an Object that doesn't have a useful toString()
method.

Then I sort of tried the next version of "RunXQuery",
which uses the "XMLPrinter" class.  I can't get that
version to compile.  But, I didn't try too hard, because
it's not what I want.  My aim is to execute an XQuery
expression in Java and get the result as a Java String.

So next I tried the approach described in "Using Qexo
with SAX2", which sounds like just what I want.

*)  I have a class named "IdentityHandler", which extends
    the SAX2 "DefaultHandler" class and implements
    ContentHandler.  This "IdentityHandler" simply
    copies the entire document that you parse.  I use
    it as a starting point for content handlers.
    It throws away processing instruction and comments,
    but othewise makes a true copy of the document
    that SAX2 scans.

*)  So, following the pattern described in "Using
    Qexo with SAX2" I specified the identity handler:

-----------------
import gnu.xquery.lang.XQuery;
import gnu.lists.*;
import gnu.kawa.sax.*;
import org.xml.sax.*;             // The main SAX package
import org.xml.sax.helpers.*;     // SAX helper classes

public class RunXQueryI
{
   public static void main (String[] args) throws Throwable
   {
      XQuery xq = new XQuery();
      IdentityHandler xih = new IdentityHandler();
      ContentConsumer cc = new ContentConsumer();
      cc.setContentHandler(xih);
      String exp = args[0];
      System.out.println("\nQuery expression:\n "+ exp );
      xq.eval(exp, cc);
      System.out.println("\nResult: \n"  + xih.resultBuffer.toString());
  }
}
--------------------
*)  To explain this:   "ContentConsumer" should turn my
    IdentityHandler into a "Consumer".  I then call the 
    version of "eval(String, Consumer)"   This should then
    call my IdentityHandler for all pieces of the result of
    the query.

*)  The result is close, but strange and incomplete.

*)  Here is a sample query:

     let $doc1 := <dept><emp> <name 
><f>Al</f><l>Adams</l></name><age>22</age></emp>
                 <emp><name><f>Bob</f><l>Baker</l></name><age>33</age></emp>  
                 
<emp><name><f>Carl</f><l>COoper</l></name><age>44</age></emp></dept>
      return $doc1

*)   When I execute this interactively, using the "--xquery" flag 
     on the kawa jar,  I get the right answer:

<dept><emp> <name><f>Al</f><l>Adams</l></name><age>22</age></emp>   
<emp><name><f>Bob</f><l>Baker</l></name><age>33</age></emp>  
<emp><name><f>Carl</f><l>COoper</l></name><age>44</age></emp></dept>

*)  When I execute it with the above "RunXQueryI" I get the following:

<dept><emp> <name><f></f>Al<l></l></name>Adams<age></age></emp>22   
<emp><name><f></f>Bob<l></l></name>Baker<age></age></emp>33       
<emp><name><f></f>Carl<l></l></name>COoper<age></age></emp></dept>

*)  Now, I've tested and used my "IdentityHandler" class elsewhere, 
    so the above leads me to conclude that the "eval(String,  Consumer)"
    method is calling the SAX2 methods out of order, and not calling all
    of them.

    It's calling the "characters" method after the corresponding "endElement" 
method.

    It's not calling the "characters" method for the "44" value of the
    "<age>" element.

    By the way:  It doesn't call the "startDocument" or "endDocument" at
    all.

*)  Anyway, sorry for the long note.

    I think that calling XQuery expressions from within Java, which I'm
    trying to do, will be a good use of the Qexo stuff.  And, I'll
    be happy to send along my "IdentityHandler" class for the common pool.

    Am I missing something or taking a wrong approach to the general
    aim of evaluating an XQuery expression in Java and getting the
    result as a String in Java?


Thanks,

Phil Shaw












  /** Evaluate a string and write the result value(s) to a Consumer. */
  public final void eval (String string, Consumer out) throws Throwable

reply via email to

[Prev in Thread] Current Thread [Next in Thread]