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 贝叶斯…...
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...
从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(九)
设备树移植 和uboot设备树修改的内容同步到kernel将设备树stm32mp157d-stm32mp157daa1-mx.dts复制到内核源码目录下 源码修改及编译 修改arch/arm/boot/dts/st/Makefile,新增设备树编译 stm32mp157f-ev1-m4-examples.dtb \stm32mp157d-stm32mp157daa1-mx.dtb修改…...
Matlab | matlab常用命令总结
常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...
涂鸦T5AI手搓语音、emoji、otto机器人从入门到实战
“🤖手搓TuyaAI语音指令 😍秒变表情包大师,让萌系Otto机器人🔥玩出智能新花样!开整!” 🤖 Otto机器人 → 直接点明主体 手搓TuyaAI语音 → 强调 自主编程/自定义 语音控制(TuyaAI…...
【HarmonyOS 5 开发速记】如何获取用户信息(头像/昵称/手机号)
1.获取 authorizationCode: 2.利用 authorizationCode 获取 accessToken:文档中心 3.获取手机:文档中心 4.获取昵称头像:文档中心 首先创建 request 若要获取手机号,scope必填 phone,permissions 必填 …...
DeepSeek 技术赋能无人农场协同作业:用 AI 重构农田管理 “神经网”
目录 一、引言二、DeepSeek 技术大揭秘2.1 核心架构解析2.2 关键技术剖析 三、智能农业无人农场协同作业现状3.1 发展现状概述3.2 协同作业模式介绍 四、DeepSeek 的 “农场奇妙游”4.1 数据处理与分析4.2 作物生长监测与预测4.3 病虫害防治4.4 农机协同作业调度 五、实际案例大…...
免费PDF转图片工具
免费PDF转图片工具 一款简单易用的PDF转图片工具,可以将PDF文件快速转换为高质量PNG图片。无需安装复杂的软件,也不需要在线上传文件,保护您的隐私。 工具截图 主要特点 🚀 快速转换:本地转换,无需等待上…...
STM32HAL库USART源代码解析及应用
STM32HAL库USART源代码解析 前言STM32CubeIDE配置串口USART和UART的选择使用模式参数设置GPIO配置DMA配置中断配置硬件流控制使能生成代码解析和使用方法串口初始化__UART_HandleTypeDef结构体浅析HAL库代码实际使用方法使用轮询方式发送使用轮询方式接收使用中断方式发送使用中…...
comfyui 工作流中 图生视频 如何增加视频的长度到5秒
comfyUI 工作流怎么可以生成更长的视频。除了硬件显存要求之外还有别的方法吗? 在ComfyUI中实现图生视频并延长到5秒,需要结合多个扩展和技巧。以下是完整解决方案: 核心工作流配置(24fps下5秒120帧) #mermaid-svg-yP…...
书籍“之“字形打印矩阵(8)0609
题目 给定一个矩阵matrix,按照"之"字形的方式打印这个矩阵,例如: 1 2 3 4 5 6 7 8 9 10 11 12 ”之“字形打印的结果为:1,…...
