I am trying to use XQuery and BaseX to manage some files.xml as databases.
I am using basexclient to send queries to basexserver. For example I have a simple file called library.xml like the following:
<library>
<book>gone with the wind</book>
<book>the thornbirds</book>
</library>
and I would like to add a node, so from basexclient
terminal I send:
XQUERY insert node <book>Dracula</book> as last into doc('library.xml')//library
and then I get the message
Query executed in 10.5 ms
But nothing has changed into the file. Is there a command to commit changes?
Performant updates to (serialized) XML files are barely possible. BaseX and also the other XML databases on the market all use special, internal representations with indexes for fast querying, and especially updates.
If you already created a database from the file, your query updates the database, not the original XML file. You should be able to observe the changes (possibly multiple times, one node added for each attempt to run the query) in the database representation, which you can verify by running
doc('library.xml')
and checking its contents. To serialize the changes to a file, use BaseX' EXPORT
command.
If you didn't create a database yet and query a file on your hard disk, a temporary in-memory database gets created, which indeed is successfully updated - but instantly discarded. Create a database before running updates.
See Christian Grün's answer discussing the WRITEBACK
option, wich enables you to update a file without creating databases.