I'm looking to run arbitrary XQuery code from Java. Oracle gives this example:
OXQDataSource ds = new OXQDataSource();
XQConnection con = ds.getConnection();
String query = "<hello-world>{1 + 1}</hello-world>";
XQPreparedExpression expr = con.prepareExpression(query);
XQSequence result = expr.executeQuery();
Should that not also be possible through BaseX
? To my understanding, BaseX is quite compliant with XQuery.
From the CLI arbitrary XQuery
code:
thufir@dur:~/basex$
thufir@dur:~/basex$ basex fetch.books.html.xq
[warning] /usr/bin/basex: Unable to locate /usr/share/java/jing.jar in /usr/share/java
<html xmlns="http://www.w3.org/1999/xhtml" class="no-js" lang="en-us">
<head>
<title>
All products | Books to Scrape - Sandbox
</title>
..
</body>
</html>thufir@dur:~/basex$
thufir@dur:~/basex$
thufir@dur:~/basex$ cat fetch.books.html.xq
fetch:xml(
'http://books.toscrape.com/',
map {
'parser': 'html',
'htmlparser': map { 'nons': false() }
}
)
thufir@dur:~/basex$
Other, non-proprietary XQuery
code runs through BaseX
as expected.
While this will populate the database:
package org.basex.examples.local;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.Databases;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.List;
import org.basex.core.cmd.Set;
import org.basex.util.list.StringList;
public class Scraper {
private static final Logger LOG = Logger.getLogger(App.class.getName());
private Properties properties = new Properties();
private URL url = null;
private String databaseName = null;
private Context context = null;
private String parserType = null;
private Scraper() {
}
public Scraper(Properties properties) {
this.properties = properties;
LOG.fine(properties.toString());
}
public void init() throws MalformedURLException {
parserType = properties.getProperty("parserType");
url = new URL(properties.getProperty(parserType + "URL"));
databaseName = properties.getProperty("databaseName");
context = new Context();
}
private void list() throws BaseXException {
LOG.info(new List().execute(context));
}
private void drop() throws BaseXException {
list();
new Set("parser", parserType).execute(context);
new CreateDB(databaseName, url.toString()).execute(context);
list();
}
private void create() throws BaseXException {
list();
new Set("parser", parserType).execute(context);
new CreateDB(databaseName, url.toString()).execute(context);
LOG.info(new List().execute(context));
list();
}
private void infoOnDatabases() {
Databases databases = context.databases();
StringList stringListOfDatabases = databases.listDBs();
String currentDatabaseName = null;
Iterator<String> databaseIterator = stringListOfDatabases.iterator();
while (databaseIterator.hasNext()) {
currentDatabaseName = databaseIterator.next();
LOG.info(currentDatabaseName);
//xQuery here..
}
}
public void fetch() throws BaseXException, MalformedURLException {
drop();
create();
infoOnDatabases();
list();
context.close();
}
}
it's rather limited.
reference:
http://docs.basex.org/wiki/Java_Examples
https://stackoverflow.com/a/44638635/262852
https://docs.oracle.com/database/121/ADXDK/adx_j_xqj.htm#ADXDK115