Sam Ben
Sam Ben

Reputation: 3

How to query eXist db from java


I'm trying to query a file based on the eXist database.
Through a simple function to display the contents of the file, no problem:

XMLResource res = (XMLResource) col.getResource(resourceName); 
System.out.println(res.getContent()); 


But when I try against making a request impossible.

String xQuery = "for $x in doc(\"" + resourceName + "\")." + "return data($x).";
ResourceSet result = service.query(xQuery); 
ResourceIterator i = result.getIterator();



I have the following errors:

Exception in thread "main" org.xmldb.api.base.XMLDBException: Failed to invoke method queryP in class org.exist.xmlrpc.RpcConnection: org.exist.xquery.StaticXQueryException: exerr:ERROR org.exist.xquery.XPathException: exerr:ERROR err:XPST0003 in line 1, column 58: unexpected token: .

at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:114)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:71)
at ExistAccess.main(ExistAccess.java:45)
Caused by: org.apache.xmlrpc.XmlRpcException: Failed to invoke method queryP in class org.exist.xmlrpc.RpcConnection: org.exist.xquery.StaticXQueryException: exerr:ERROR org.exist.xquery.XPathException: exerr:ERROR err:XPST0003 in line 1, column 58: unexpected token: .

at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:197)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156)
at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:158)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:147)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:99)
... 2 more
[[email protected]: Failed to invoke method queryP in class org.exist.xmlrpc.RpcConnection: org.exist.xquery.StaticXQueryException: exerr:ERROR org.exist.xquery.XPathException: exerr:ERROR err:XPST0003 in line 1, column 58: unexpected token: .



I checked all my .jar file, and all of them are present... I really need help ! Thanks in advance!

Upvotes: 0

Views: 1858

Answers (2)

Joe Wicentowski
Joe Wicentowski

Reputation: 5294

Your query:

String xQuery = "for $x in doc(\"" + resourceName + "\")." + "return data($x).";

The core of the error:

err:XPST0003 in line 1, column 58: unexpected token: .

As the error message states, eXist-db recognizes an error with the "."; this period/dot is invalid XQuery. Remove the dot from the query, and you should be fine. The query text itself should look like this:

for $x in doc("/db/mycollection/mydocument.xml") return data($x)

Also, it appears your FLWOR loop is iterating over a single item - the resource. Therefore, the FLWOR is extraneous. You could refactor this as:

data(doc("/db/mycollection/mydocument.xml"))

Upvotes: 1

TimonWang
TimonWang

Reputation: 920

I think you string concat make this issue, why not try to add a space after ".". Change your code like

    String xQuery = "for $x in doc(\"" + resourceName + "\"). " + "return data($x).";

Upvotes: 0

Related Questions