我正在寻找从Java运行任意XQuery代码的方法。 Oracle给出了以下示例:
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();
通过BaseX
也不可能吗?据我了解,BaseX与XQuery相当compliant。
从CLI任意XQuery
代码中:
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$
其他非专有XQuery
代码按预期通过BaseX
运行。
这将填充数据库:
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();
}
}
相当有限。
参考:
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
答案 0 :(得分:0)
最简单的查询结果:
thufir@dur:~/NetBeansProjects/helloWorldBaseX$
thufir@dur:~/NetBeansProjects/helloWorldBaseX$ gradle clean run
> Task :run
Jan 02, 2019 7:44:16 PM org.basex.examples.local.DatabaseQuery runQuery
INFO: name Resources Size Input Path
----------------------------------------------------------------------
w3school_data 1 5178 https://www.w3schools.com/xml/note.xml
1 Databases.
Jan 02, 2019 7:44:16 PM org.basex.examples.local.DatabaseQuery runQuery
INFO: Don't forget me this weekend!
Jan 02, 2019 7:44:16 PM org.basex.examples.local.DatabaseQuery runQuery
INFO: query was //note/body/text()
BUILD SUCCESSFUL in 1s
4 actionable tasks: 3 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/helloWorldBaseX$
Java:
package org.basex.examples.local;
import java.net.MalformedURLException;
..
import org.basex.core.cmd.XQuery;
public class DatabaseQuery {
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 DatabaseQuery() {
}
public DatabaseQuery(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();
}
public void runQuery(String query) throws BaseXException {
new Open(databaseName).execute(context);
LOG.info(new List().execute(context));
LOG.info(new XQuery(query).execute(context));
LOG.info("query was\t\t\t" + query);
context.close();
}
}
(同一类,一个配置参数获取xml
,这对我来说更易于查询。)
query
方法来自: