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.