At present, many open-source organizations and companies have implemented query Engines Based on the XQuery reference standards. The Representative open-source XQuery engines are XQEngine, Saxon, Galax, and other open-source XQuery engines, dataDirect company's DataDirect Xquery.
1. Install datadirectxquery. jar to obtainDdxq. jarFile
To use Xquery in java, XQJ is required. Therefore, you need to download datadirectxquery. jar, which is shared in my personal resources. Run the command in cmd after the download.
Jar xvf datadirectxquery. jar, and double-clickXQueryInstaller. jarFile. If Java is installed in the system, the GUI installer is enabled. Install the SDK by default. 2. compile a java program, run it in eclipse, create a new Java Project, right-click the Project point Property, click Java Build Path on the left, select Libraries, and click Add External Jars, select ddxq In the lib folder where you install xquery. jar. Create an XQueryTester. java file to create/access the XQuery data source and execute the XQuery. The specific content of the XQueryTester. java file is
import javax.xml.namespace.QName;import java.util.Properties;import com.ddtek.xquery3.XQConnection;import com.ddtek.xquery3.XQException;import com.ddtek.xquery3.XQExpression;import com.ddtek.xquery3.XQItemType;import com.ddtek.xquery3.XQSequence;import com.ddtek.xquery3.xqj.DDXQDataSource;public class XQueryTester { // Filename for XML document to query private String filename; // Data Source for querying private DDXQDataSource dataSource; // Connection for querying private XQConnection conn; public XQueryTester(String filename) { this.filename = filename; } public void init() throws XQException { dataSource = new DDXQDataSource(); conn = dataSource.getConnection(); } public String query(String queryString) throws XQException { XQExpression expression = conn.createExpression(); expression.bindString(new QName("docName"), filename, conn.createAtomicType(XQItemType.XQBASETYPE_STRING)); XQSequence results = expression.executeQuery(queryString); return results.getSequenceAsString(new Properties()); } public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java ibm.dw.xqj.XQueryTester [XML filename]"); System.exit(-1); } try { String xmlFilename = args[0]; XQueryTester tester = new XQueryTester(xmlFilename); tester.init(); final String sep = System.getProperty("line.separator"); String queryString = "declare variable $docName as xs:string external;" + sep + " for $cd in doc($docName)/CATALOG/CD " + " where $cd/YEAR > 1980 " + " and $cd/COUNTRY = 'USA' " + " order by $cd/YEAR " + " return " + "
{$cd/TITLE/text()}" + "
{$cd/YEAR/text()}
"; System.out.println(tester.query(queryString)); } catch (Exception e) { e.printStackTrace(System.err); System.err.println(e.getMessage()); } }}