简体   繁体   中英

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. After reading those files I will output the data into the database table (some RDBMS). But I am lost as to where to start as I am not able to find much tutorials about working with 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. To access the documents , use collection("yourdbname") . If you need to access a particular file, use 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. For storing data in an RDBMS using SQL, have a look at the SQL module , there are also some examples included.

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

which has an introduction to Xquery and FLWOR

You might prefer the graphical front-end over the console (text) interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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