XQuery创建BaseX数据库实例

news/2024/11/17 13:39:03/

XQuery创建BaseX数据库实例

文章目录

  • XQuery创建BaseX数据库实例
    • 1、准备工作
    • 2、demo目录结构
    • 3、IDEA配置BaseX
    • 4、工具类BaseXClient
    • 5、Example

1、准备工作

开发工具:

  • IDEA
  • Oxygen

技术:

  • Java
  • BaseX
  • Xpath
  • Xquery

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目录结构

img

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

3、IDEA配置BaseX

img

img

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();}}
}

img


http://www.ppmy.cn/news/1041580.html

相关文章

前端如何安全的渲染HTML字符串?

在现代的Web 应用中&#xff0c;动态生成和渲染 HTML 字符串是很常见的需求。然而&#xff0c;不正确地渲染HTML字符串可能会导致安全漏洞&#xff0c;例如跨站脚本攻击&#xff08;XSS&#xff09;。为了确保应用的安全性&#xff0c;我们需要采取一些措施来在安全的环境下渲染…

【BASH】回顾与知识点梳理(三十六)

【BASH】回顾与知识点梳理 三十六 三十六. 认识与分析登录档36.1 什么是登录档CentOS 7 登录档简易说明登录档的重要性Linux 常见的登录档档名登录档所需相关服务 (daemon) 与程序CentOS 7.x 使用 systemd 提供的 journalctl 日志管理 登录档内容的一般格式 36.2 rsyslog.servi…

C++之类之间访问函数指针(一百八十一)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

阿里巴巴常用的12个后端开发工具

1 阿尔萨斯Java在线诊断工具 Arthas是一款用于Java应用程序的在线诊断工具&#xff0c;由阿里巴巴于2018年9月开源。 典型场景&#xff1a; 您不知道从中加载类的特定JAR包。 您想弄清楚为什么您的系统会抛出各种与类相关的异常。 您不知道为什么修改后的代码无法执行。您不…

韦东山-电子量产工具项目:UI系统

代码结构 所有代码都已通过测试跑通&#xff0c;其中代码结构如下&#xff1a; 一、include文件夹 1.1 common.h #ifndef _COMMON_H #define _COMMON_Htypedef struct Region {int iLeftUpX; //区域左上方的坐标int iLeftUpY; //区域左下方的坐标int iWidth; //区域宽度…

《算法竞赛·快冲300题》每日一题:“圆内的最短距离”

《算法竞赛快冲300题》将于2024年出版&#xff0c;是《算法竞赛》的辅助练习册。 所有题目放在自建的OJ New Online Judge。 用C/C、Java、Python三种语言给出代码&#xff0c;以中低档题为主&#xff0c;适合入门、进阶。 文章目录 题目描述题解C代码Java代码Python代码 “ 圆…

SpringBoot2【学习记录一】

springboot优点&#xff1a; 创建独立spring应用内嵌web服务器自动starter依赖&#xff0c;简化构建配置自动配置spring以及第三方功能提供生产级别的监控&#xff0c;健康检查及外部化配置无代码生成&#xff0c;无需编写XML springboot缺点&#xff1a; 版本迭代快&#x…

Spring Boot实现IP地址解析

一、本地解析 如果使用本地ip解析的话&#xff0c;我们将会借助ip2region&#xff0c;该项目维护了一份较为详细的本地ip地址对应表&#xff0c;如果为了离线环境的使用&#xff0c;需要导入该项目依赖&#xff0c;并指定版本&#xff0c;不同版本的方法可能存在差异。 <d…