XQuery创建BaseX数据库实例
XQuery创建BaseX数据库实例
文章目录
- XQuery创建BaseX数据库实例
- 1、准备工作
- 2、demo目录结构
- 3、IDEA配置BaseX
- 4、工具类BaseXClient
- 5、Example
1、准备工作
开发工具:
IDEAOxygen
技术:
JavaBaseXXpathXquery
BaseX需要阅读的文档:
- https://github.com/BaseXdb/basex/blob/9/basex-examples/src/main/java/org/basex/examples/api/Example.java
- https://docs.basex.org/wiki/Table_of_Contents
2、demo目录结构

Base X相当于一个工具类,Example是我们写的创建XML数据库的例子。
3、IDEA配置BaseX


4、工具类BaseXClient
package com.linghu.util;import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;/*** Java client for BaseX.* Works with BaseX 7.0 and later** Documentation: https://docs.basex.org/wiki/Clients** (C) BaseX Team 2005-22, BSD License*/
public final class BaseXClient implements Closeable {/** UTF-8 charset. */private static final Charset UTF8 = Charset.forName("UTF-8");/** Output stream. */private final OutputStream out;/** Input stream (buffered). */private final BufferedInputStream in;/** Socket. */private final Socket socket;/** Command info. */private String info;/*** Constructor.* @param host server name* @param port server port* @param username user name* @param password password* @throws*/public BaseXClient(final String host, final int port, final String username,final String password) throws IOException {socket = new Socket();socket.setTcpNoDelay(true);socket.connect(new InetSocketAddress(host, port), 5000);in = new BufferedInputStream(socket.getInputStream());out = socket.getOutputStream();// receive server responsefinal String[] response = receive().split(":");final String code, nonce;if(response.length > 1) {// support for digest authenticationcode = username + ':' + response[0] + ':' + password;nonce = response[1];} else {// support for cram-md5 (Version < 8.0)code = password;nonce = response[0];}send(username);send(md5(md5(code) + nonce));// receive success flagif(!ok()) throw new IOException("Access denied.");}/*** Executes a command and serializes the result to an output stream.* @param command command* @param output output stream* @throws IOException Exception*/public void execute(final String command, final OutputStream output) throws IOException {// send {Command}0send(command);receive(in, output);info = receive();if(!ok()) throw new IOException(info);}/*** Executes a command and returns the result.* @param command command* @return result* @throws IOException Exception*/public String execute(final String command) throws IOException {final ByteArrayOutputStream os = new ByteArrayOutputStream();execute(command, os);return new String(os.toByteArray(), UTF8);}/*** Creates a query object.* @param query query string* @return query* @throws IOException Exception*/public Query query(final String query) throws IOException {return new Query(query);}/*** Creates a database.* @param name name of database* @param input xml input* @throws IOException I/O exception*/public void create(final String name, final InputStream input) throws IOException {send(8, name, input);}/*** Adds a document to a database.* @param path path to resource* @param input xml input* @throws IOException I/O exception*/public void add(final String path, final InputStream input) throws IOException {send(9, path, input);}/*** Replaces a document in a database.* @param path path to resource* @param input xml input* @throws IOException I/O exception*/public void replace(final String path, final InputStream input) throws IOException {send(12, path, input);}/*** Stores a binary resource in a database.* @param path path to resource* @param input xml input* @throws IOException I/O exception*/public void store(final String path, final InputStream input) throws IOException {send(13, path, input);}/*** Returns command information.* @return string info*/public String info() {return info;}/*** Closes the session.* @throws IOException Exception*/@Overridepublic void close() throws IOException, IOException {send("exit");out.flush();socket.close();}/*** Checks the next success flag.* @return value of check* @throws IOException Exception*/private boolean ok() throws IOException {out.flush();return in.read() == 0;}/*** Returns the next received string.* @return String result or info* @throws IOException I/O exception*/private String receive() throws IOException {final ByteArrayOutputStream os = new ByteArrayOutputStream();receive(in, os);return new String(os.toByteArray(), UTF8);}/*** Sends a string to the server.* @param string string to be sent* @throws IOException I/O exception*/private void send(final String string) throws IOException {out.write((string + '\0').getBytes(UTF8));}/*** Receives a string and writes it to the specified output stream.* @param input input stream* @param output output stream* @throws IOException I/O exception*/private static void receive(final InputStream input, final OutputStream output)throws IOException {for(int b; (b = input.read()) > 0;) {// read next byte if 0xFF is receivedoutput.write(b == 0xFF ? input.read() : b);}}/*** Sends a command, argument, and input.* @param code command code* @param path name, or path to resource* @param input xml input* @throws IOException I/O exception*/private void send(final int code, final String path, final InputStream input) throws IOException {out.write(code);send(path);send(input);}/*** Sends an input stream to the server.* @param input xml input* @throws IOException I/O exception*/private void send(final InputStream input) throws IOException {final BufferedInputStream bis = new BufferedInputStream(input);final BufferedOutputStream bos = new BufferedOutputStream(out);for(int b; (b = bis.read()) != -1;) {// 0x00 and 0xFF will be prefixed by 0xFFif(b == 0x00 || b == 0xFF) bos.write(0xFF);bos.write(b);}bos.write(0);bos.flush();info = receive();if(!ok()) throw new IOException(info);}/*** Returns an MD5 hash.* @param pw String* @return String*/private static String md5(final String pw) {final StringBuilder sb = new StringBuilder();try {final MessageDigest md = MessageDigest.getInstance("MD5");md.update(pw.getBytes());for(final byte b : md.digest()) {final String s = Integer.toHexString(b & 0xFF);if(s.length() == 1) sb.append('0');sb.append(s);}} catch(final NoSuchAlgorithmException ex) {// should not occurex.printStackTrace();}return sb.toString();}/*** Inner class for iterative query execution.*/public class Query implements Closeable {/** Query id. */private final String id;/** Cached results. */private ArrayList<byte[]> cache;/** Cache pointer. */private int pos;/*** Standard constructor.* @param query query string* @throws IOException I/O exception*/Query(final String query) throws IOException {id = exec(0, query);}/*** Binds a value to an external variable.* @param name name of variable* @param value value* @throws IOException I/O exception*/public void bind(final String name, final String value) throws IOException {bind(name, value, "");}/*** Binds a value with the specified type to an external variable.* @param name name of variable* @param value value* @param type type (can be an empty string)* @throws IOException I/O exception*/public void bind(final String name, final String value, final String type) throws IOException {cache = null;exec(3, id + '\0' + name + '\0' + value + '\0' + type);}/*** Binds a value to the context item.* @param value value* @throws IOException I/O exception*/public void context(final String value) throws IOException {context(value, "");}/*** Binds a value with the specified type to the context item.* @param value value* @param type type (can be an empty string)* @throws IOException I/O exception*/public void context(final String value, final String type) throws IOException {cache = null;exec(14, id + '\0' + value + '\0' + type);}/*** Checks for the next item.* @return result of check* @throws IOException I/O exception*/public boolean more() throws IOException {if(cache == null) {out.write(4);send(id);cache = new ArrayList<>();final ByteArrayOutputStream os = new ByteArrayOutputStream();while(in.read() > 0) {receive(in, os);cache.add(os.toByteArray());os.reset();}if(!ok()) throw new IOException(receive());pos = 0;}if(pos < cache.size()) return true;cache = null;return false;}/*** Returns the next item.* @return item string* @throws IOException I/O Exception*/public String next() throws IOException {return more() ? new String(cache.set(pos++, null), UTF8) : null;}/*** Returns the whole result of the query.* @return query result* @throws IOException I/O Exception*/public String execute() throws IOException {return exec(5, id);}/*** Returns query info in a string.* @return query info* @throws IOException I/O exception*/public String info() throws IOException {return exec(6, id);}/*** Returns serialization parameters in a string.* @return query info* @throws IOException I/O exception*/public String options() throws IOException {return exec(7, id);}/*** Closes the query.* @throws IOException I/O exception*/@Overridepublic void close() throws IOException {exec(2, id);}/*** Executes the specified command.* @param code command code* @param arg argument* @return resulting string* @throws IOException I/O exception*/private String exec(final int code, final String arg) throws IOException {out.write(code);send(arg);final String s = receive();if(!ok()) throw new IOException(receive());return s;}}
}
5、Example
接下来开始创建数据库
package com.linghu.util;import java.io.IOException;
import java.io.OutputStream;/*** This example shows how commands can be executed on a server.** This example requires a running database server instance.* Documentation: https://docs.basex.org/wiki/Clients** @author BaseX Team 2005-22, BSD License*/
public final class Example {/*** Main method.* @param args command-line arguments* @throws IOException I/O exception*/public static void main(final String... args) throws IOException {// create sessiontry(BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin")) {session.query("db:create('Zhang')").execute();}}
}

相关文章:
XQuery创建BaseX数据库实例
XQuery创建BaseX数据库实例 文章目录 XQuery创建BaseX数据库实例1、准备工作2、demo目录结构3、IDEA配置BaseX4、工具类BaseXClient5、Example 1、准备工作 开发工具: IDEAOxygen 技术: JavaBaseXXpathXquery BaseX需要阅读的文档: htt…...
MySQL索引(Index)
Index 数据库中的索引(Index)是一种数据结构,用于提高数据库查询性能和加速数据检索过程。索引可以看作是数据库表中某个或多个列的数据结构,类似于书中的目录,可以帮助数据库管理系统更快地定位和访问数据。它们是数…...
web框架面试题
1、Django 的生命周期? 前端发起请求nginxuwsgi中间件URLview视图通过orm与model层进行数据交互拿到数据返回给view试图将数据渲染到模板中拿到字符串中间件uwsginginx前端渲染 2、中间件的五种方法? process_requestprocess_responseProcess_viewPro…...
什么是字体堆栈(font stack)?如何设置字体堆栈?
聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是字体堆栈(Font Stack)?⭐ 如何设置字体堆栈?⭐ 写在最后 ⭐ 专栏简介 前端入门之旅:探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 …...
推特群推王:引爆您的产品
作为出海市场的营销平台,Twitter的流量不断攀升,已然成为跨境贸易企业的一部分。当前,Twitter已不再是一个简单的社交平台,而是一个强大的营销平台,使企业能够与受众实时互动。然而,与其他社交媒体一样&…...
[JavaWeb]【七】web后端开发-MYSQL
前言:MySQL是一种流行的关系型数据库管理系统,它的作用是存储和管理数据。在Web开发中,MySQL是必备的数据库技能之一,因为它可以帮助Web开发人员处理大量的数据,并且提供了强大的数据查询和管理功能。 一 数据库介绍 1.1 什么是数据库 1.2 数据库产品 二 MySQL概述…...
C语言:初阶测试错题(查漏补缺)
题一:字符串倒置 示例1 输入 I like beijing. 输出 beijing. like I 思路一: 定义字符串数组arr[ ] ,利用gets()将要倒置的字符串输入,记录字符串长度len,此时写一个逆置函数Inversion(),第一步将整个字符串逆置&…...
数组累加器-reduce、reduceRight
数组累加器-reduce 一、基本语法1.reduce2.reduceRight 二、具体使用1.reduce2.reduceRight 三、使用场景1.数组求和2.数组求积3.计算数组中每个元素出现的次数 一、基本语法 1.reduce reduce() :对数组中的每个元素按序执行一个提供的 reducer 函数,每…...
uniapp 官方扩展组件 uni-combox 实现:只能选择不能手写(输入中支持过滤显示下拉列表)
uniapp 官方扩展组件 uni-combox 实现:只能选择不能手写(输入中支持过滤显示下拉列表) uni-comboxuni-combox 原本支持:问题: 改造源码参考资料 uni-combox uni-combox 原本支持: 下拉选择。输入关键字&am…...
TypeScript 语法
环境搭建 以javascript为基础构建的语言,一个js的超集,可以在任何支持js的平台中执行,ts扩展了js并且添加了类型,但是ts不能被js解析器直接执行,需要编译器编译为js文件,然后引入到 html 页面使用。 ts增…...
已经开源的中文大模型对比,支持更新
大模型下载:互链高科 ClueAI/PromptCLUE-base-v1-5 at main (huggingface.co) 支持多任务生成,支持中文,不支持多轮对话,体验:ClueAI (cluebenchmarks.com) 基于promptclue-base进一步训练的模型:ClueAI/Ch…...
调用其他页面onload函数的方法
在微信小程序中,可以通过以下方法来触发其他页面的 onLoad 函数执行: 使用全局事件订阅机制:在 App 实例中定义一个全局事件,在需要触发的地方发布该事件,在每个页面的 onLoad 函数中订阅该事件,并在回调函…...
视频怎么转换成gif表情包?三步完成视频在线转gif
小伙伴们在使用gif表情包的时候,都会注意到有些是视频片段,其实视频转换成gif动图已经很常见了,今天就来给大家演示一下使用视频转gif工具(https://www.gif.cn)来将视频在线转gif,一起来学习一下吧。 打开…...
ElasticSearch安装与介绍
Elastic Stack简介 如果没有听说过Elastic Stack,那你一定听说过ELK,实际上ELK是三款软件的简称,分别是Elasticsearch、 Logstash、Kibana组成,在发展的过程中,又有新成员Beats的加入,所以就形成了Elastic…...
每天一道leetcode:剑指 Offer 36. 二叉搜索树与双向链表(中等深度优先遍历递归)
今日份题目: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。 示例 我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于…...
基于docker搭建pytest自动化测试环境(docker+pytest+jenkins+allure)
pytest搭建自动化测试环境(dockerpytestjenkinsallure) 这里我以ubuntu18为例 如果有docker环境,可以直接拉取我打包好的镜像docker pull ziyigun/jenkins:v1.0 1 搭建Docker 1.1 安装docker # 配置docker安装环境 sudo apt-get install ap…...
Debian 10驱动Broadcom 无线网卡
用lspci命令查询无线网卡品牌: 运行下面代码后,重启即可。 apt-get install linux-image-$(uname -r|sed s,[^-]*-[^-]*-,,) linux-headers-$(uname -r|sed s,[^-]*-[^-]*-,,) broadcom-sta-dkms...
系统架构设计师---2018年下午试题1分析与解答(试题二)
2018年下午试题1分析与解答 试题二 阅读以下关于软件系统建模的叙述,在答题纸上回答问题 1 至问题 3。 【说明】 某公司欲建设一个房屋租赁服务系统,统一管理房主和租赁者的信息,提供快捷的租赁服务。本系统的主要功能描述如下: 1. 登记房主信息。记录房主的姓名、住址…...
移远通信推出一站式Matter解决方案,构建智能家居开放新生态
近日,全球领先的S物联网整体解决方案供应商移远通信宣布,正式推出全新Matter解决方案,从模组、APP、平台、认证、生产五大层面为客户提供一站式服务,赋能智能家居行业加快融合发展。 过去十年,得益于物联网生态的发展&…...
文本挖掘 day5:文本挖掘与贝叶斯网络方法识别化学品安全风险因素
文本挖掘与贝叶斯网络方法识别化学品安全风险因素 1. Introduction现实意义理论意义提出方法,目标 2. 材料与方法2.1 数据集2.2 数据预处理2.3 关键字提取2.3.1 TF-IDF2.3.2 改进的BM25——BM25WBM25BM25W 2.3.3 关键词的产生(相关系数) 2.4 关联规则分析2.5 贝叶斯…...
多模态2025:技术路线“神仙打架”,视频生成冲上云霄
文|魏琳华 编|王一粟 一场大会,聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中,汇集了学界、创业公司和大厂等三方的热门选手,关于多模态的集中讨论达到了前所未有的热度。其中,…...
UE5 学习系列(三)创建和移动物体
这篇博客是该系列的第三篇,是在之前两篇博客的基础上展开,主要介绍如何在操作界面中创建和拖动物体,这篇博客跟随的视频链接如下: B 站视频:s03-创建和移动物体 如果你不打算开之前的博客并且对UE5 比较熟的话按照以…...
浅谈不同二分算法的查找情况
二分算法原理比较简单,但是实际的算法模板却有很多,这一切都源于二分查找问题中的复杂情况和二分算法的边界处理,以下是博主对一些二分算法查找的情况分析。 需要说明的是,以下二分算法都是基于有序序列为升序有序的情况…...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
JS手写代码篇----使用Promise封装AJAX请求
15、使用Promise封装AJAX请求 promise就有reject和resolve了,就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...
Linux nano命令的基本使用
参考资料 GNU nanoを使いこなすnano基础 目录 一. 简介二. 文件打开2.1 普通方式打开文件2.2 只读方式打开文件 三. 文件查看3.1 打开文件时,显示行号3.2 翻页查看 四. 文件编辑4.1 Ctrl K 复制 和 Ctrl U 粘贴4.2 Alt/Esc U 撤回 五. 文件保存与退出5.1 Ctrl …...
python爬虫——气象数据爬取
一、导入库与全局配置 python 运行 import json import datetime import time import requests from sqlalchemy import create_engine import csv import pandas as pd作用: 引入数据解析、网络请求、时间处理、数据库操作等所需库。requests:发送 …...
LOOI机器人的技术实现解析:从手势识别到边缘检测
LOOI机器人作为一款创新的AI硬件产品,通过将智能手机转变为具有情感交互能力的桌面机器人,展示了前沿AI技术与传统硬件设计的完美结合。作为AI与玩具领域的专家,我将全面解析LOOI的技术实现架构,特别是其手势识别、物体识别和环境…...
c# 局部函数 定义、功能与示例
C# 局部函数:定义、功能与示例 1. 定义与功能 局部函数(Local Function)是嵌套在另一个方法内部的私有方法,仅在包含它的方法内可见。 • 作用:封装仅用于当前方法的逻辑,避免污染类作用域,提升…...
pycharm 设置环境出错
pycharm 设置环境出错 pycharm 新建项目,设置虚拟环境,出错 pycharm 出错 Cannot open Local Failed to start [powershell.exe, -NoExit, -ExecutionPolicy, Bypass, -File, C:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\terminal\shell-int…...
