I am trying to use the Java BaseX XQJ API to insert data into a XML-file.
The code is as follows (imports are ommited):
public class BaseXTest {
public static void main(String[] args) throws Exception {
// obtain an XQDataSource instance
XQDataSource ds = new BaseXXQDataSource();
ds.setProperty("serverName", "localhost");
ds.setProperty("port", "1984");
ds.setProperty("user", "admin");
ds.setProperty("password", "admin");
XQConnection xqc = ds.getConnection();
XQExpression xqe = xqc.createExpression();
xqe.executeCommand("CREATE DB myTestDB");
ds.setProperty("databaseName", "myTestDB");
File myTestDbDataFile = new File("PATH_TO_MY_XML_FILE");
XQConnection2 xqc2 = (XQConnection2) ds.getConnection();
XQItem xqItem = xqc2.createItemFromDocument(new FileInputStream(myTestDbDataFile), null, null);
xqc2.insertItem(myTestDbDataFile.getName(), xqItem, null);
//this query works
// XQResultSequence rs = xqe
// .executeQuery("for $i in (1 to 10) return $i");
//this query works too
// XQResultSequence rs =
// xqe.executeQuery("doc('PATH_TO_MY_XML_FILE')/myTestDbRootNode");
//this query works NOT
XQResultSequence rs = xqe.executeQuery("insert node 'abcdefg' into doc('PATH_TO_MY_XML_FILE')/myTestDbRootNode");
rs.writeSequence(System.out, null);
xqc.close();
}
}
The above code throws this exception (in line with the statement 'rs.writeSequence(System.out, null);'):
Exception in thread "main" javax.xml.xquery.XQException:
XQJFOS021 - FORWARD_ONLY_SEQUENCE: Cursor is not positioned on an XQItem.
at XmlTestMain.main(XmlTestMain.java:118)
Why is this the case and how can i issue a working "insert"-directive to the database?
I guess there is something wrong with the usage of the "executeQuery()-method" in case of an insert, since it can process the other two directives issued.
I guess the query is actually working. However, what return value do you actually expect?
The XQuery Update statement you execute does not return any result (see https://docs.basex.org/wiki/Updates#Returning_Results for more information), thus the result is not an XQItem.
Also, if you want to modify main-memory nodes (like reading them using fn:doc) you might want to turn the WRITEBACK option on. If you actually want to transform the document and return the result back to you application, you might want to use the transform expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With