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 贝叶斯…...
Claude Markdown增强资源库:提升AI文档生成质量与效率
1. 项目概述:为什么我们需要一个“Claude Markdown 增强”资源库? 如果你和我一样,是 Claude 的深度用户,并且经常用它来辅助编程、撰写文档或整理知识,那你一定遇到过这个痛点:Claude 输出的 Markdown 代…...
Axure RP中文界面完整汉化指南:3分钟免费安装全系列版本
Axure RP中文界面完整汉化指南:3分钟免费安装全系列版本 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 对于中文用户…...
PADS PCB设计工具的核心优势与应用实践
1. PADS PCB设计工具概述作为一名拥有十年PCB设计经验的工程师,我亲身体验过从Protel到Altium再到Cadence Allegro的各种EDA工具。但当我在2015年首次接触PADS时,它独特的"约束驱动设计"理念和高效的交互式布线引擎立刻吸引了我。PADS…...
Java 资源释放与堆外内存管理机制演进分析
在 Java 虚拟机(JVM)的内存管理模型中,垃圾收集器(GC)仅负责回收 JVM 堆内存(Heap Memory)中不可达对象所占用的空间。然而,Java 程序在运行过程中必然会涉及到不受 GC 直接控制的外…...
从零构建C++/CUDA推理引擎:深入解析yalm项目与LLM底层优化
1. 项目概述:从零构建一个高性能的C/CUDA推理引擎最近在深入研究大语言模型推理的性能优化,发现很多开源实现为了追求极致的性能,代码往往高度优化,甚至引入了动态并行等高级CUDA特性,这对想深入理解底层原理的开发者来…...
模块三-数据清洗与预处理——14. 重复值处理
14. 重复值处理 1. 概述 重复值是数据中的常见问题,可能来自数据录入错误、系统重复导出、数据合并等原因。重复数据会导致统计偏差、模型过拟合,需要在数据预处理阶段处理。 import pandas as pd import numpy as np# 创建包含重复值的示例数据 df pd.…...
零代码部署 OpenClaw:Win11 一键安装与使用教程
OpenClaw(小龙虾)Windows 11 一键部署教程 2026 最新版 零代码免配置解压即用适用系统:Windows 11 专业版 / 家庭版 / 正式版(全版本兼容) 项目介绍:OpenClaw 是 GitHub 星标 28W 的开源本地 AI 智能体&am…...
CipherGuard:编译器级密文侧信道攻击防护技术解析
1. CipherGuard技术背景与核心挑战密文侧信道攻击(Ciphertext Side-Channel Attacks)已成为现代可信执行环境(TEE)中最棘手的安全威胁之一。这类攻击不直接破解加密算法本身,而是通过分析加密操作执行过程中产生的内存…...
全栈开发真的是万能解药吗?3年全栈开发者的血泪教训
一、从测试视角看全栈热:光环下的误解作为软件测试从业者,你一定不止一次在行业论坛、招聘启事里看到“全栈开发”这四个字。它像一个自带聚光灯的概念,被描绘成能独当一面解决所有技术问题的“万能解药”——前端页面布局、后端逻辑搭建、数…...
企业知识管理新方案:OpenCorpo开源项目部署与RAG架构实践
1. 项目概述与核心价值最近在折腾一个挺有意思的开源项目,叫 OpenCorpo。这名字听起来有点“高大上”,但说白了,它就是一个帮你把公司内部那些零散、混乱的文档、知识、流程给“盘活”的工具。想象一下,你公司里是不是有无数个共享…...
