简体   繁体   English

使用 BaseX 读取多个 XML 文件

[英]Reading Multiple XML Files with BaseX

Hi I am new to BaseX and I am trying to read a bunch of XML files from a folder.嗨,我是 BaseX 的新手,我正在尝试从文件夹中读取一堆 XML 文件。 After reading those files I will output the data into the database table (some RDBMS).读取这些文件后,我会将数据输出到数据库表(一些 RDBMS)中。 But I am lost as to where to start as I am not able to find much tutorials about working with BaseX.但是我不知道从哪里开始,因为我找不到很多关于使用 BaseX 的教程。 I had searched on internet but still not much help.我已经在互联网上搜索过,但仍然没有太大帮助。 Can someone please help me with this.有人可以帮我解决这个问题。

Thanks in advance.提前致谢。

Use CREATE DB yourdbname /path/to/folder to create a database containing all documents in this folder.使用CREATE DB yourdbname /path/to/folder创建包含此文件夹中所有文档的数据库 To access the documents , use collection("yourdbname") .访问文档,请使用collection("yourdbname") If you need to access a particular file, use collection("yourdbname/document.xml") .如果您需要访问特定文件,请使用collection("yourdbname/document.xml")

To query all these files, you could do something like要查询所有这些文件,您可以执行以下操作

for $document in collection("yourdbname")
return string-join((
    document-uri($document),
    ": ",
    xs:string(count($document//*))
  ))

which will return all document paths with their respecting node counts.这将返回所有文档路径及其对应的节点数。

For further reading, have a look at the getting started section in BaseX' documentation , you should be able to find all needed information in there.如需进一步阅读,请查看BaseX 文档中的入门部分,您应该能够在其中找到所有需要的信息。 For storing data in an RDBMS using SQL, have a look at the SQL module , there are also some examples included.要使用 SQL 在 RDBMS 中存储数据,请查看SQL 模块,其中还包含一些示例。

adding a few files individually:单独添加几个文件:

nicholas@mordor:~/flwor/bookstore$ 
nicholas@mordor:~/flwor/bookstore$ cat book1.xml 
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>


nicholas@mordor:~/flwor/bookstore$ 
nicholas@mordor:~/flwor/bookstore$ cat book2.xml 
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>


nicholas@mordor:~/flwor/bookstore$ 
nicholas@mordor:~/flwor/bookstore$ basex
BaseX 9.0.1 [Standalone]
Try 'help' to get more information.
> 
> create database books
Database 'books' created in 225.89 ms.
> 
> open books
Database 'books' was opened in 0.04 ms.
> 
> set parser xml
PARSER: xml
> 
> add book1.xml
Resource(s) added in 160.76 ms.
> 
> add book2.xml
Resource(s) added in 4.86 ms.
> 
> xquery /book/title
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
Query executed in 188.71 ms.
> 
> exit
Have a nice day.
nicholas@mordor:~/flwor/bookstore$ 

data from:数据来自:

https://www.w3schools.com/xml/books.xml https://www.w3schools.com/xml/books.xml

which has an introduction to Xquery and FLWOR其中介绍了 Xquery 和 FLWOR

You might prefer the graphical front-end over the console (text) interface.您可能更喜欢图形前端而不是控制台(文本)界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM