How to Use Xquery ---- datadirectxquery in java programs

Source: Internet
Author: User
Tags xquery

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());    }  }}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.