BaseX with Java
BaseX with Java
Tags
Archive
INTRODUCTION
In order to search for a document in a baseX database, the following code is used
doc('pathIntroduintNomBD') . Example
doc('BD/economia/empresa.xml')
Keep in mind that the Bd is the name of the database that is stored in baseX, and
the subsequent path is where an item can be found in that database.
To look up all the documents in a created database path we use the code
for $i in collection('path') return document-uri($i) . Example
for $i in collection('/practica')
return document-uri($i)
If the path isn’t indicated. BabseX will show all the documents in the opened
Database.
If the insertion is only for one node then the following should be used instead
If inserting into only one node then there is no use to use for
❗Because we are inserting into a node that exist we use the ((as (first | last))? into)
syntax.
❗ When inserting an attribute and a node into a node we just put the both of
them in a single parenthesis,and use the after like we inserting a node and put
the location at the end because we inserting the new address node into the
location node. When inserting mutiple attributes and node it still follows the
structure and separate the attributes with a coma
insert node (
attribute fax {'938040000'},
attribute email {'[email protected]'},
<adreça>
<carrer>Av. Emili Vallès, 4</carrer>
<cp>08700</cp>
</adreça>,
<telefon>123456789</telefon>
)
after doc('/Practica/economia/empresa.xml')//dept[@codi='d50']/localitat
In our database since we have one only one node called adreca in the department
called adrca we can use the rename without FLOWR but if there are many in other
nodes that is the wrong way. The correct way is as follows.
for $n in doc('/Practica/economia/empresa.xml')//dept/direccion
return rename node $n as 'domicili'
for $n in doc('/Practica/economia/empresa.xml')//dept/@tel
return rename node $n as 'telefon'
for $n in nodeOAtributAReanomenar
return replace value of node $n with 'nouValor'
❗Make sure to choose the node specifically or else it will change all the nodes
for $n in nodeOAtributAReanomenar
return replace node $n with nouNodeONouAtribut
To indicate the new attribute the norm of the INSERT command should be
followed.
Delete- node(s)/attribute(s)
for $n in nodeOAtributAEliminar
return delete node $n
connection.close();
Username of a db admin
Password of a db admin
Queries In BaseX
To make queries in BaseX the ClientQuery class is used where you inject the
current connection to it by means of connection.query() .
To execute a query passed into the clientquery by the connection the clientquery
has the execute() method that returns the query result from the Db
After each query the clientquery should be closed to release resources through
the close() method
ClientQuery q = connection.query(query);
Let's break this method eliminarEmpleat into digestible parts to explain the logic
clearly, focusing on the actCap == 0 case and the else block.
Parameters:
Case: actCap == 0
When actCap == 0 , the subordinates of the employee being removed are left without
a manager. Here’s what happens:
removes the cap attribute from all subordinates of the employee identified by
codi . This means those subordinates no longer have a manager.
while (iteEmp.hasNext()) {
Empleat e = iteEmp.next();
if (e.getCap() != null && e.getCap().getCodi() == codi) {
e.setCap(null);
}
}
1. Validation:
if (this.esSubordinatDirecteIndirecte(codi, actCap)) {
throw new EPSednaException(...);
}
If any validations fail, exceptions are thrown to ensure data consistency and to
prevent logical errors like circular references.