Monday, October 11, 2010

XQJ - Java API for XQuery

The XQuery language allows queries to be executed against individual XML documents or collections of XML documents. The results of queries are instances of the XML Query Data Model. These instances include simple values (e.g. numbers, strings), XML nodes, and sequences of both values and XML nodes. XQuery can operate on physical XML documents or virtual XML documents that have been derived from sources of data such as relational or object databases.

XQJ (JSR 225) is a standard Java API for interacting with a variety of XQuery engines operating on XML data sources. JSR 225 specification defines a set of interfaces and classes that enable Java applications to submit XQuery queries against one or more XML data sources to an XQuery engine and consume the results.

Eg:

// obtain an XQDataSource instance
   XQDataSource xqds = (XQDataSource)
      Class.forName("com.jsr225.xqj").newInstance();
  
   // obtain a connection
   XQConnection con = xqds.getConnection("usr", "passwd");
  
   // prepare an XQuery Expression
   String xqry = "for $i in fn:collection('dept') " +
      "where $i/deptname = %dname return count($i/employees)";
   XQPreparedExpression expr = con,preparedExpression(xqry);
  
   // bind variable with value
   expr.bindString(new Qname("dname"), "engineering");
  
   // execute the XQuery Expression
   XQResultSequence rs = expr.executeQuery();
  
   // Consume results
   while (rs.next())
   {
     System.out.printLn(rs.getInt());
   }
  
   // clean up resources
   rs.close();
   con.close();


The package structure of the API would be:
    javax.xml.xquery

Misc:
JSR 225

No comments:

Post a Comment