繁体   English   中英

如何使用BaseX从Java运行XQuery?

[英]How to run an XQuery from Java using BaseX?

我正在寻找从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。

从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://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/RunQueries.java

https://stackoverflow.com/a/44638635/262852

https://docs.oracle.com/database/121/ADXDK/adx_j_xqj.htm#ADXDK115

最简单的查询结果:

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方法来自:

https://github.com/BaseXdb/basex/blob/master/basex-examples/src/main/java/org/basex/examples/local/RunQueries.java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM