1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
import java.io.File;
import java.io.FileNotFoundException;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.dbxml.XmlContainer;
import com.sleepycat.dbxml.XmlContainerConfig;
import com.sleepycat.dbxml.XmlDocument;
import com.sleepycat.dbxml.XmlException;
import com.sleepycat.dbxml.XmlManager;
import com.sleepycat.dbxml.XmlManagerConfig;
import com.sleepycat.dbxml.XmlQueryContext;
import com.sleepycat.dbxml.XmlQueryExpression;
import com.sleepycat.dbxml.XmlResults;
import com.sleepycat.dbxml.XmlValue;
public class QueryData {
public static void main (String[] args)
{
Environment env = null;
XmlManager manager = null;
File envHome = new File("C:/xprojet/XData");
try {
EnvironmentConfig envConf = new EnvironmentConfig();
envConf.setErrorStream(System.err);
envConf.setAllowCreate( true ); // If the environment does not exits, create it.
envConf.setInitializeCache( true ); // Turn on the shared memory region.
envConf.setInitializeLocking( true ); // Turn on the locking subsystem.
envConf.setInitializeLogging( true ); // Turn on the logging subsystem.
envConf.setTransactional( true ); // Turn on the transactional subsystem.
//use system memory (set to true for some unix ntfs)
envConf.setSystemMemory(true);
env = new Environment( envHome, envConf );
XmlManagerConfig conf = new XmlManagerConfig();
XmlContainerConfig contenerConf = new XmlContainerConfig();
contenerConf.setReadOnly(true);
contenerConf.setIndexNodes(true);
contenerConf.setTransactional(true);
contenerConf.setNodeContainer(true);
conf.setAdoptEnvironment( true );
conf.setAllowAutoOpen( true );
manager = new XmlManager( env, conf );
//test l'existence du contener
envConf.setRunRecovery(true);
envConf.setAllowCreate(true);
conf.setAllowExternalAccess(true);
manager.setDefaultContainerType( XmlContainer.NodeContainer );
envHome = new File( manager.getHome() );
//Get a query context
XmlQueryContext context = manager.createQueryContext();
// Set the evaluation type to Lazy.
context.setEvaluationType(XmlQueryContext.Lazy);
// Declare the query string
String queryString =
"for $u in collection('data.dbxml')/orgs/organization"
+ "return $u";
// Prepare (compile) the query
XmlQueryExpression qe = manager.prepare(queryString, context);
// Declare a variable. Note that this method really wants an XmlValue
// object as the variable's argument. However, we just give it a
// string here and allow XmlValue's string constructor to create
// the XmlValue object for us.
//context.setVariableValue("member", new XmlValue("member"));
// Run the query. Note that you can perform this steps many times
// without suffering the overhead of re-creating the query expression.
XmlResults results = qe.execute(context);
System.out.println("ok");
System.out.println(results);
// Show the size of the result set
String message = "Found ";
message += results.size() + " documents for query: '";
message += queryString + "'\n";
System.out.println(message);
// Display the result set
XmlValue value = results.next();
System.out.println("ok");
while (value != null) {
XmlDocument theDoc = value.asDocument();
//String docName = theDoc.getName();
//String docString = value.asString();
message = "Document ";
message += theDoc.getName() + ":\n";
message += value.asString();
message += "===============================\n";
System.out.println(message);
results.delete();
value = results.next();
System.out.println("ok");
theDoc.delete();
value.delete();
qe.delete();
}
}
catch (XmlException e)
{
e.printStackTrace();
}
catch (FileNotFoundException f)
{
f.printStackTrace();
} catch (DatabaseException e) {
e.printStackTrace();
}
finally
{
try {
if (manager != null){
manager.close();
}
if (env != null){
env.close();
}
}catch (XmlException ce) {
ce.printStackTrace();
} catch (DatabaseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}} |
Partager