Monday, August 14, 2006

FLWOR with Berkeley DB XML

FLWOR (pronounced Flower) stands for "for let while order by return" and is and XQuery expression. FLWOR enables advanced querying. More details about
FLWOR can be found at http://www.w3schools.com/xquery/xquery_flwor.asp

Sleepycat's Berkeley's XML Database, DB XML the native XML database supports XQuery. I have tried to create a small example here to demonstrate how to read XQuery with FLWOR expressions from a file, and run them against the database of Berkeley DBs dbxml container.(the one built in the 'gettingStarted' section) It is assumed that the xml sample data from the getting started kit has been loaded.

Set your classpath to include the Berkley DB jars under jar folder
(db.jar, dbxml.jar, dbexamples.jar)

The code can be found here.

1. query.txt file, which holds the query.



for $doc in collection(\'simpleExampleData.dbxml\')

where $doc/product/item[text()=\"Lemon Grass\"]

return $doc/product



2. FLWOR.java file, which holds the code.



import java.io.*;

import com.sleepycat.dbxml.*;

import com.sleepycat.db.*;

import dbxml.gettingStarted.*;



public class FLWOR {

 private static void usage() {

String usageMessage = \"\\nThis program performs queries against a DBXML container.\\n\";

usageMessage += \"You should run exampleLoadContainer before running this example.\\n\";

usageMessage += \"You are only required to pass this command the path location of the database\\n\";

usageMessage += \"environment that you specified when you loaded the examples data:\\n\";

usageMessage += \" and the query file which holds the query\\n\";

usageMessage += \"\\t-h <dbenv directory> -q <queryFile>\\n\";



usageMessage += \"For example:\\n\";

usageMessage += \"\\tjava ch.inform.bdb.FLWOR -h examplesEnvironment -q query.txt\\n\";



System.out.println(usageMessage);

System.exit( -1 );

 }



 

 //Utility function to clean up objects, exceptions or not,

 // containers and environments must be closed.

 private static void cleanup(myDbEnv env, XmlContainer openedContainer) {

try {

    if (openedContainer != null)

openedContainer.close();

    if (env != null)

env.cleanup();

} catch (Exception e) {

    // ignore exceptions on close

}

 }



 public static void main(String args[])

throws Throwable {

String theContainer = null;

File path2DbEnv = null;

File queryFile = null;

for(int i = 0; i < args.length; ++i) {

         if (args[i].startsWith(\"-\")) {

          switch(args[i].charAt(1)) {

           case \'h\':

           path2DbEnv = new File(args[++i]);

           break;

           case \'q\':

           queryFile = new File(args[++i]);

           break;

           default:

           usage();

           }//switch

         }//if

}//for



if (path2DbEnv == null || ! path2DbEnv.isDirectory()) {

         usage();

}



if (queryFile == null || queryFile.isDirectory()) {

System.out.println(\"queryFile is \" + queryFile);

usage();

}



//Stream to read file

BufferedReader fin;

String query = \"\";

try

{

fin = 

            new BufferedReader(new FileReader(queryFile));

String line;

    while ((line = fin.readLine()) != null ) {

     query += \"\\n\" + line;

    }



    // Close our input stream

    fin.close();

}

// Catches any error conditions

catch (IOException e)

{

System.err.println (\"Unable to read from file\");

System.exit(-1);

}



System.out.println(\"Query is \" + query);



myDbEnv env = null;

XmlTransaction txn = null;

XmlContainer openedContainer = null;

try {

    env = new myDbEnv(path2DbEnv);

    XmlManager theMgr = env.getManager();

    String containerMark = \"\\\'\";

    theContainer = query.substring(

     query.indexOf(containerMark)+1, 

     query.lastIndexOf(containerMark));

    System.out.println(\"Container is \" + theContainer);

    //Open a non-transactional container

    openedContainer =

     theMgr.openContainer(theContainer);

    XmlQueryContext resultsContext = theMgr.createQueryContext();



        XmlResults results = theMgr.query(query.trim(),

      resultsContext);

    XmlValue value = results.next();

    while (value != null) {

    //Pull the value out of the document query result set.

    System.out.println(value.asString());

    }



} catch (Exception e) {

         System.err.println(\"Error performing query against \" + theContainer);

         System.err.println(\"   Message: \" + e.getMessage());

         throw e;

    }

    finally {

   cleanup(env, openedContainer);

    }

 } //End main

}



I am assuming you have already run the examples. So after that, please compile and
say the following at the command line at test folder:
java FLWOR -h -q query.txt

Labels: , ,

0 Comments:

Post a Comment

<< Home