Difference between revisions of "Java Examples"

From BaseX Documentation
Jump to navigation Jump to search
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
This page is part of the [[Developer Section]].
 
This page is part of the [[Developer Section]].
The following Java code snippets demonstrate how easy it is to run database commands, create collections, perform queries, etc. by integrating the BaseX code. Most examples are taken from our [https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples basex-examples] repository, in which you will find some more use cases.
+
The following Java code snippets demonstrate how easy it is to run database commands, create collections, perform queries, etc. by integrating the BaseX code. Most examples are taken from our [https://github.com/BaseXdb/basex/tree/master/basex-examples/src/main/java/org/basex/examples/ basex-examples] repository, in which you will find some more use cases.
  
 
<div style="float:left; width:48%;">
 
<div style="float:left; width:48%;">
Line 7: Line 7:
 
The following code snippets work in ''embedded'' mode; they do not rely on an additional server instance:
 
The following code snippets work in ''embedded'' mode; they do not rely on an additional server instance:
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/RunCommands.java RunCommands.java]<br/>creates and drops database and index instances, prints a list of all existing databases.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/RunCommands.java RunCommands.java]<br/>creates and drops database and index instances, prints a list of all existing databases.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/RunQueries.java RunQueries.java]<br/>shows three variants of running queries.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/RunQueries.java RunQueries.java]<br/>shows three variants of running queries.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/BindContext.java BindContext.java]<br/>demonstrates how a value can be bound as context item.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/BindContext.java BindContext.java]<br/>demonstrates how a value can be bound as context item.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/BindVariables.java BindVariables.java]<br/>demonstrates how a value can be bound to a variable.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/BindVariables.java BindVariables.java]<br/>demonstrates how a value can be bound to a variable.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/CreateCollection.java CreateCollection.java]<br/>creates and manages a collection.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/CreateCollection.java CreateCollection.java]<br/>creates and manages a collection.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/QueryCollection.java QueryCollection.java]<br/>creates, runs queries against it and drops a collection.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/QueryCollection.java QueryCollection.java]<br/>creates, runs queries against it and drops a collection.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/local/WikiExample.java WikiExample.java]<br/>creates a database from an url (wiki instance), runs a query against it and drops the database.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/WikiExample.java WikiExample.java]<br/>creates a database from an url (wiki instance), runs a query against it and drops the database.
  
 
==Server Examples==
 
==Server Examples==
Line 19: Line 19:
 
The examples below take advantage of the client/server architecture:
 
The examples below take advantage of the client/server architecture:
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/ServerCommands.java ServerCommands.java]<br/>launches server-side commands using a client session.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/server/ServerCommands.java ServerCommands.java]<br/>launches server-side commands using a client session.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/ServerAndLocal.java ServerAndLocal.java]<br/>processes server results locally.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/server/ServerAndLocal.java ServerAndLocal.java]<br/>processes server results locally.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/ServerConcurrency.java ServerConcurrency.java]<br/>runs concurrent queries.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/server/ServerConcurrency.java ServerConcurrency.java]<br/>runs concurrent queries.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/ServerQueries.java ServerQueries.java]<br/>shows how iterative queries can be performed.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/server/ServerQueries.java ServerQueries.java]<br/>shows how iterative queries can be performed.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/ServerEventsGUI.java ServerEventsGUI.java]<br/>is a little GUI example for demonstrating database events.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/server/UserExample.java UserExample.java]<br/>manages database users.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/server/UserExample.java UserExample.java]<br/>manages database users.
 
  
 
==XQuery Module Examples==
 
==XQuery Module Examples==
Line 30: Line 29:
 
BaseX provides [[Java Bindings]] for accessing external Java code via XQuery functions. The following examples show how this feature can be utilized:
 
BaseX provides [[Java Bindings]] for accessing external Java code via XQuery functions. The following examples show how this feature can be utilized:
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/module/FruitsExample.java FruitsExample.java]<br/>demonstrates how Java classes can be imported as XQuery modules.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/module/FruitsExample.java FruitsExample.java]<br/>demonstrates how Java classes can be imported as XQuery modules.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/module/FruitsModule.java FruitsModule.java]<br/>is a simple demo module called by {{Code|FruitsExample}}.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/module/FruitsModule.java FruitsModule.java]<br/>is a simple demo module called by {{Code|FruitsExample}}.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/module/ModuleDemo.java ModuleDemo.java]<br/>is a simple XQuery demo module that demonstrates how XQuery items can be processed from Java. It is derived from the {{Code|QueryModule}} class.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/module/ModuleDemo.java ModuleDemo.java]<br/>is a simple XQuery demo module that demonstrates how XQuery items can be processed from Java. It is derived from the {{Code|QueryModule}} class.
* [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/query/QueryModule.java QueryModule.java]<br/>is located in the BaseX core. Java query modules can extend this class to get access to the current query context and enrich functions with properties ().
+
* [https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/query/QueryModule.java QueryModule.java]<br/>is located in the BaseX core. Java query modules can extend this class to get access to the current query context and enrich functions with properties ().
  
 
</div><div style="float:left; width:4%;">&nbsp;
 
</div><div style="float:left; width:4%;">&nbsp;
 
</div><div style="float:left; width:48%;">
 
</div><div style="float:left; width:48%;">
 
==XQJ API==
 
 
The implementation of the [http://xqj.net/basex/ BaseX XQJ API] (closed-source) has been written by Charles Foster. It uses the client/server architecture.
 
The basex-examples repository contains [https://github.com/BaseXdb/basex-examples/tree/master/src/main/java/org/basex/examples/xqj various examples] on how to use XQJ.
 
  
 
==[[Clients|Client API]]==
 
==[[Clients|Client API]]==
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/BaseXClient.java BaseXClient.java]<br/>provides an implementation of the [[Server Protocol]].
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/BaseXClient.java BaseXClient.java]<br/>provides an implementation of the [[Server Protocol]].
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/Example.java Example.java]<br/>demonstrates how commands can be executed on a server.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/Example.java Example.java]<br/>demonstrates how commands can be executed on a server.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/QueryExample.java QueryExample.java]<br/>shows how queries can be executed in an iterative manner.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/QueryExample.java QueryExample.java]<br/>shows how queries can be executed in an iterative manner.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/QueryBindExample.java QueryBindExample.java]<br/>shows how external variables can be bound to XQuery expressions.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/QueryBindExample.java QueryBindExample.java]<br/>shows how external variables can be bound to XQuery expressions.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/CreateExample.java CreateExample.java]<br/>shows how new databases can be created.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/CreateExample.java CreateExample.java]<br/>shows how new databases can be created.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/AddExample.java AddExample.java]<br/>shows how documents can be added to databases, and how existing documents can be replaced.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/AddExample.java AddExample.java]<br/>shows how documents can be added to databases, and how existing documents can be replaced.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/EventExample.java EventExample.java]<br/>demonstrates how to trigger and receive database events.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/api/BinaryExample.java BinaryExample.java]<br/>shows how binary resource can be added to and retrieved from the database.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/api/BinaryExample.java BinaryExample.java]<br/>shows how binary resource can be added to and retrieved from the database.
 
  
 
==[[REST API]]==
 
==[[REST API]]==
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/rest/RESTGet.java RESTGet.java]<br/>presents the HTTP GET method.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/rest/RESTGet.java RESTGet.java]<br/>presents the HTTP GET method.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/rest/RESTPost.java RESTPost.java]<br/>presents the HTTP POST method.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/rest/RESTPost.java RESTPost.java]<br/>presents the HTTP POST method.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/rest/RESTPut.java RESTPut.java]<br/>presents the HTTP PUT method.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/rest/RESTPut.java RESTPut.java]<br/>presents the HTTP PUT method.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/rest/RESTAll.java RESTAll.java]<br/>runs all examples at one go.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/rest/RESTAll.java RESTAll.java]<br/>runs all examples at one go.
  
 
==XML:DB API (deprecated)==
 
==XML:DB API (deprecated)==
Line 65: Line 58:
 
Note that the XML:DB API does not talk to the server and can thus only be used in embedded mode.
 
Note that the XML:DB API does not talk to the server and can thus only be used in embedded mode.
  
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/xmldb/XMLDBCreate.java XMLDBCreate.java]<br/>creates a collection using XML:DB.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/xmldb/XMLDBCreate.java XMLDBCreate.java]<br/>creates a collection using XML:DB.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/xmldb/XMLDBQuery.java XMLDBQuery.java]<br/>runs a query using XML:DB.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/xmldb/XMLDBQuery.java XMLDBQuery.java]<br/>runs a query using XML:DB.
* [https://github.com/BaseXdb/basex-examples/blob/master/src/main/java/org/basex/examples/xmldb/XMLDBInsert.java XMLDBInsert.java]<br/>inserts a document into a database using XML:DB.
+
* [https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/xmldb/XMLDBInsert.java XMLDBInsert.java]<br/>inserts a document into a database using XML:DB.
 +
 
 +
==XQJ API (closed source)==
 +
 
 +
The implementation of the [http://xqj.net/basex/ BaseX XQJ API] has been written by Charles Foster. The basex-examples repository contains [https://github.com/BaseXdb/basex/tree/master/basex-examples/src/main/java/org/basex/examples/xqj various examples] on how to use XQJ.
 
</div>
 
</div>
 
[[Category:Developer]]
 
 
__NOTOC__
 
__NOTOC__

Latest revision as of 09:42, 6 September 2020

This page is part of the Developer Section. The following Java code snippets demonstrate how easy it is to run database commands, create collections, perform queries, etc. by integrating the BaseX code. Most examples are taken from our basex-examples repository, in which you will find some more use cases.

Local Examples[edit]

The following code snippets work in embedded mode; they do not rely on an additional server instance:

Server Examples[edit]

The examples below take advantage of the client/server architecture:

XQuery Module Examples[edit]

BaseX provides Java Bindings for accessing external Java code via XQuery functions. The following examples show how this feature can be utilized:

  • FruitsExample.java
    demonstrates how Java classes can be imported as XQuery modules.
  • FruitsModule.java
    is a simple demo module called by FruitsExample.
  • ModuleDemo.java
    is a simple XQuery demo module that demonstrates how XQuery items can be processed from Java. It is derived from the QueryModule class.
  • QueryModule.java
    is located in the BaseX core. Java query modules can extend this class to get access to the current query context and enrich functions with properties ().
 

Client API[edit]

REST API[edit]

XML:DB API (deprecated)[edit]

Note that the XML:DB API does not talk to the server and can thus only be used in embedded mode.

XQJ API (closed source)[edit]

The implementation of the BaseX XQJ API has been written by Charles Foster. The basex-examples repository contains various examples on how to use XQJ.