5

I want to use XQuery for fetching data from Xml in java, but I am not getting which Jar I need to add for this. I searched in google lot but haven't got any useful example. For example I got following link:

https://docs.oracle.com/database/121/ADXDK/adx_j_xqj.htm

Then I am not getting jar file mentioned here. Anyone know how to start using XQuery in Java with a simple example will be very helpful. After lot of search I have got this link which is saying Xqjapi.jar comes with oracle database.

https://technology.amis.nl/2011/07/30/running-xquery-from-java-applications-using-the-xqj-api-and-the-oracle-xdk-implementation/

Is there any open source api I can use in Java for using XQuery?

JohnE
  • 29,156
  • 8
  • 79
  • 109
RSingh
  • 171
  • 2
  • 12
  • http://www.oracle.com/technetwork/database/index-100632.html – Anantha Sharma May 23 '15 at 13:32
  • thanks for answering,But As far I have read,If i need to use oracle library,oracle database should be there.here I want to create a simple java class and there i want to fetch a element of xml using xquery. – RSingh May 23 '15 at 13:42
  • @AnanthaSharma plz see the comment above – RSingh May 23 '15 at 13:51
  • @AnanthaSharma please see the below link- https://technology.amis.nl/2011/07/30/running-xquery-from-java-applications-using-the-xqj-api-and-the-oracle-xdk-implementation/ – RSingh May 23 '15 at 14:06
  • well even I have no good news as such, just read in this link here, https://community.oracle.com/thread/2556726, you need to install database for related jars, but still looking, will update here if found something useful. Also can you change one line in your question, `Anyone know how to start using jquery in java with a simple example will be very helpfull.` ., I guess it should be `XQuery` and not `jquery`. – Shrikant Havale May 23 '15 at 14:21
  • @ShrikantHavale thanks for your comment.I will update it sure. – RSingh May 23 '15 at 16:07
  • I think it cud be used also-http://www.tutorialspoint.com/xquery/xquery_environment.htm – RSingh May 23 '15 at 16:35

2 Answers2

6

Well, there is another approach to get the required JARS, you need not install oracle database for that. I found the solution in this link,

http://orafmwsoa.blogspot.co.at/2014/06/running-xquery-from-java-applications.html

But you need not go through the entire link, what important point to note is, all necessary JARS are available from Oracle XQuery for Hadoop implementation.

And here is the link,

Oracle XQuery for Hadoop 2.4.1

Just download Oracle XQuery for Hadoop 4.1.0 and extract and create a normal Java Project with the libraries in path.

I tried this example from your link, and it works

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQSequence;
import oracle.xml.xquery.OXQDataSource;


public class HelloWorld {

public static void main(String[] args) throws XQException {
    OXQDataSource ds = new OXQDataSource();
    XQConnection con = ds.getConnection();
    String query = "<hello-world>{1 + 1}</hello-world>";
    XQPreparedExpression expr = con.prepareExpression(query); 
    XQSequence result = expr.executeQuery();
    System.out.println(result.getSequenceAsString(null));
    
    result.close();
    expr.close();
    con.close();
}

}

With following JARS in path,

apache-xmlbeans

orai18n-mapping

oxquery

xmlparserv2_sans_jaxp_services

xqjapi

Also another suggestion,

  1. I installed Oracle Express Edition 11.2 OracleXE112_Win64 and it didn't had the required JARS, XE may be has limited functionality, so try installing different one.
  2. Also this link here Embedding XQuery in Java suggests usage of SAXON XSLT & XQuery processor. SAXON
Community
  • 1
  • 1
Shrikant Havale
  • 1,250
  • 1
  • 16
  • 36
0

If the XML is in filestore, then you need a filestore-based XQuery processor such as Saxon.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 2
    To expand on this answer - XQJ is just an API, and it is one of several different APIs that you could use for processing XQuery from Java. If you have a few documents on a filesystem then you can use a file based processor such as Saxon. If you use Saxon. I would suggest using its S9API as opposed to XQJ though as it is a nicer fit. If you have many XML files then you might want to consider a Native XML Database, in which instance as you are using Java I would suggest eXist or BaseX; The eXist O'Reilly book has a Java API chapter. – adamretter May 23 '15 at 19:10