added 11
[williamcs420.git] / assign11.aux / CeisQuery.java
blob9b46435a97fd21d20abff7aeac4451343f1b3719
1 import com.sleepycat.db.Environment;
2 import com.sleepycat.db.EnvironmentConfig;
3 import com.sleepycat.dbxml.*;
5 import java.io.File;
7 public class CeisQuery {
9 private void run() {
10 Environment myEnv;
11 File envHome = new File("xmlEnvironment");
12 XmlManager myManager = null;
13 XmlContainer myContainer = null;
15 try {
16 /* Create the environment. */
17 EnvironmentConfig envConf = new EnvironmentConfig();
18 envConf.setAllowCreate(false);
19 envConf.setInitializeCache(true);
20 myEnv = new Environment(envHome, envConf);
22 /* Create the manager. */
23 XmlManagerConfig managerConfig = new XmlManagerConfig();
24 managerConfig.setAdoptEnvironment(true);
25 myManager = new XmlManager(myEnv, managerConfig);
27 /* Open the container. */
28 myContainer = myManager.openContainer("ceis.dbxml");
30 // Get a query context
31 XmlQueryContext context = myManager.createQueryContext();
32 context.setReturnType(XmlQueryContext.LiveValues);
33 context.setEvaluationType(XmlQueryContext.Eager);
35 // Declare the query string
36 String myQuery = "for $s in doc(\"dbxml:ceis.dbxml/ceis.xml\")/ceis/students/student return <studentid>{$s/studentid/string()}</studentid>";
37 XmlQueryExpression qe = myManager.prepare(myQuery, context);
38 /* Execute the query. */
39 XmlResults results = qe.execute(context);
41 /* Display the results. */
42 System.out.printf("Count=%d%n", results.size());
43 XmlValue value = results.next();
44 while (value != null) {
45 /* Get the xml returned by query. */
46 String result = value.asString();
48 /* Debug output. */
49 System.out.printf("result= %s%n", result);
51 /* Get the next value returned by the query. */
52 value = results.next();
55 /* Cleanup. */
56 results.delete();
57 qe.delete();
59 } catch (Exception e) {
60 e.printStackTrace();
61 } finally {
62 /* Close the container and the manager (manager closes environment). */
63 try {
64 if (myContainer != null) {
65 myContainer.close();
66 System.out.println("Container closed.");
68 if (myManager != null) {
69 myManager.close();
70 System.out.println("Manager closed.");
72 System.out.println("End of program.");
73 } catch (XmlException ce) {
74 ce.printStackTrace();
79 public static void main(String[] args) {
80 CeisQuery application = new CeisQuery();
81 application.run();