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 贝叶斯…...
生物认证锁:用虹膜加密核心模块——软件测试从业者的专业指南
在数字化转型浪潮中,生物认证技术正重塑安全防护体系,其中虹膜识别凭借其超高精度和防伪特性,成为加密核心模块(如支付系统、数据库访问控制或敏感API)的首选方案。作为软件测试从业者,您肩负着验证系统鲁棒…...
2026年鱼生专用花生油:哪些品牌值得选?
大家好,今天咱们聊聊一个很有趣的话题——鱼生专用花生油。说到鱼生,大家可能会想到广东、广西地区的美食,尤其是那一道道色香味俱全的鱼生,简直让人垂涎欲滴。但是,鱼生的美味离不开优质的食用油,尤其是花…...
零基础掌握SeleniumBasic:革新性浏览器自动化框架全攻略
零基础掌握SeleniumBasic:革新性浏览器自动化框架全攻略 【免费下载链接】SeleniumBasic A Selenium based browser automation framework for VB.Net, VBA and VBScript 项目地址: https://gitcode.com/gh_mirrors/se/SeleniumBasic 每天重复机械的网页操作…...
基于智能体(Agent)的自动化图像工作流:Wan2.2-I2V-A14B与任务编排
基于智能体(Agent)的自动化图像工作流:Wan2.2-I2V-A14B与任务编排 1. 引言:当图像生成遇上智能体 想象一下这样的场景:你需要为电商平台制作一组节日主题的广告图,包含特定风格的背景、商品展示和人物互动…...
ESP8266嵌入式JavaScript引擎:零内存分配的确定性JS执行
1. 项目概述 ESP8266-Arduino-JavaScript 是一个面向 ESP8266 平台的轻量级嵌入式 JavaScript 引擎库,其核心目标并非在微控制器上完整复刻 V8 或 SpiderMonkey 的功能,而是为资源受限的 IoT 设备提供一种 可预测、内存可控、无动态分配、零依赖 的脚本…...
跨平台哔哩哔哩内容管理神器:BiliTools全方位使用指南
跨平台哔哩哔哩内容管理神器:BiliTools全方位使用指南 【免费下载链接】BiliTools A cross-platform bilibili toolbox. 跨平台哔哩哔哩工具箱,支持视频、音乐、番剧、课程下载……持续更新 项目地址: https://gitcode.com/GitHub_Trending/bilit/Bili…...
QGIS3.28最新版行政区合并避坑指南:县转市数据融合的3个关键检查点
QGIS 3.28行政区合并实战:县转市数据融合的3个关键检查点 当我们需要将县级行政区数据合并为市级边界时,看似简单的"线转面融合"操作背后,往往隐藏着诸多数据陷阱。许多中级用户在QGIS中执行这类操作时,明明步骤正确却频…...
2026 ASNT-TC-1A 无损检测 Ⅱ/Ⅲ 级认证指南|API/ASME 认证必备 + 报考实操
一、行业刚需:为何 ASNT-TC-1A 资质是工业检测领域的「硬通货」在石油天然气、压力容器、钢结构焊接等工业领域,无损检测(NDT)是产品质量保障的核心环节,而ASNT-TC-1A作为美国无损检测学会制定的人员资格鉴定和认证标准…...
OpenClaw会议纪要大师:Qwen3-32B实时转录飞书语音会议
OpenClaw会议纪要大师:Qwen3-32B实时转录飞书语音会议 1. 为什么需要自动化会议纪要 每次开完会最头疼的就是整理会议纪要。作为团队的技术负责人,我每周要参加至少8场跨部门会议,传统的手动记录方式让我苦不堪言——要么记录不全重点&…...
2026-3-26、可变字符串类型StringBuilder
*为什么使用StringBuiler: string是不可变字符串类型,意味着一旦修改就无法修改: string s "Hello"; s s " World"; // 看起来是修改,实际上是创建了新对象// 原来的"Hello"对象还在内存中stri…...
