API for XQUERY

Hello!

I just need a simple source code example which it uses API & Java. It is about join and XQUERY language. I know that in Tamino Interactive Interface, I can execute query in XQUERY notation like the following one:

--------
for
$b in input()/bib/book,
$a in input()/reviews/entry
where $b/title = $a/title
return

{ $b/author}
{ $b/title }
{ $a/review }

--------

How can I use in APIs?
Simple sample code will be very appreciated.

In advance, Thanks a lot!

best regards,
Dariusz Baumann

Hello Dariusz,

the Tamino API for Java now has a new method:
   

public TResponse xquery(TXQuery xquery) throws TXQueryException



(The construction of a TXQuery instance is done in the same way as a TQuery.)

I hope that helps,
Trevor.

First of all, thanks for a quick reply Trevor! :slight_smile:

I have just found new method “xquery”, I now have been trying to use it in properly way. Unfortunately, I could not achieve any success. :frowning:

My source code is (named ProcessPersonsDOM4JX.java):

--------------------------

import com.softwareag.tamino.db.API.objectModel.TXMLObject;
import com.softwareag.tamino.db.API.objectModel.TXMLObjectModel;
import com.softwareag.tamino.db.API.objectModel.dom4j.TDOM4JObjectModel;
import com.softwareag.tamino.db.API.objectModel.TXMLObjectIterator;
import com.softwareag.tamino.db.API.objectModel.TXMLObject;

import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import org.dom4j.QName;
import org.dom4j.Namespace;

import com.softwareag.tamino.db.API.accessor.TXMLObjectAccessor;
import com.softwareag.tamino.db.API.accessor.TAccessLocation;
import com.softwareag.tamino.db.API.accessor.TQuery;
import com.softwareag.tamino.db.API.accessor.TQueryException;
import com.softwareag.tamino.db.API.accessor.TInsertException;
import com.softwareag.tamino.db.API.accessor.TDeleteException;
import com.softwareag.tamino.db.API.accessor.TAccessorException;
import com.softwareag.tamino.db.API.accessor.TSystemAccessor;

import com.softwareag.tamino.db.API.accessor.TXQuery;
import com.softwareag.tamino.db.API.accessor.TXQueryException;

import com.softwareag.tamino.db.API.io.TStreamWriteException;

import com.softwareag.tamino.db.API.connection.TConnectionException;
import com.softwareag.tamino.db.API.connection.TConnectionFactory;
import com.softwareag.tamino.db.API.connection.TConnection;

import com.softwareag.tamino.db.API.response.TResponse;

import com.softwareag.tamino.db.API.common.*;

import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.io.FileNotFoundException;
import java.io.InputStream;


public class ProcessPersonsDOM4JX {

protected static boolean checkServerAndPrintSystemInformation(TConnection Connection) throws TAccessorException {
TSystemAccessor SystemAccessor = Connection.newSystemAccessor();
if (!SystemAccessor.isServerAlive()) {
return false;
} else {
System.out.println( " server is alive" );
System.out.println( “\nHere is some systeminformation” );
System.out.println( “------------------------------\n” );
System.out.println( “The Tamino server hosting " + DATABASE_URI + " is version " + SystemAccessor.getServerVersion() );
System.out.println( “(Server API version: " + SystemAccessor.getServerAPIVersion() + “, Tamino API for Java version: " + SystemAccessor.getAPIVersion() + “)\n” );
return true;
}
}

// Helpermethod to get the text of a given tagname from a DOM4J tree
private static String getDOM4JElementTextByTagName(Element element, String tagname) {
List list = element.elements(tagname);
Element e = (Element)list.get(0);
return
e.node(0).getStringValue();
}

private static void printPerson(TXMLObject xmlObject) throws TStreamWriteException {

Element e = (Element)xmlObject.getElement();

String surname = getDOM4JElementTextByTagName(e, “surname”);
String firstname= getDOM4JElementTextByTagName(e, “firstname”);
String sex = getDOM4JElementTextByTagName(e, “sex”);
String city = getDOM4JElementTextByTagName(e, “city”);
String occupation = getDOM4JElementTextByTagName(e, “occupation”);

System.out.println( surname + “, " +
firstname +”, “+
sex +”, “+
city +”, “+
occupation);

System.out.print( " (” );
System.out.print( “ino:id="” + xmlObject.getId() +”" " );
System.out.print( “collection="” + xmlObject.getCollection() +”" " );
System.out.print( “doctype="” + xmlObject.getDoctype() +”" " );
System.out.println( “)” );
}

private static String performCountXQuery(TXMLObjectAccessor accessor, TXQuery query) throws TException {
try {
TXQuery countquery = TXQuery.newInstance( “count(” + query.getExpression() + “)” );
return accessor.xquery( countquery ).getQueryContentAsString();
}
catch (TXQueryException queryException) {
showAccessFailure( queryException );
throw queryException;
}
}

protected static void performXQueryAndListPersons(TXMLObjectAccessor accessor, TXQuery query) throws TException {
try {
TResponse response = accessor.xquery( query );
TXMLObjectIterator iterator = response.getXMLObjectIterator();
System.out.print( “The query "” + query + “" returns " );
if (!iterator.hasNext()) {
System.out.println( “no documents!” );
} else {
System.out.println( performCountXQuery( accessor, query ) + " documents, which are:” );
while (iterator.hasNext()) {
TXMLObject xmlObject = iterator.next();
System.out.print( " " );
printPerson(xmlObject);
}
}
} catch (TXQueryException queryException) {
showAccessFailure( queryException );
throw queryException;
}
}

private static void showAccessFailure(TAccessorException accessorException) {
TAccessFailureMessage accessFailure = accessorException.getAccessFailureMessage();
if ( accessFailure != null )
System.out.println( “Access failed with:” + accessFailure );
else
System.out.println( “Access failed:” + accessorException.getMessage() );
}

public static void main(String args) throws TException {
System.out.print( "Connecting to Tamino database " + DATABASE_URI + “, …” );
TConnectionFactory connectionFactory = TConnectionFactory.getInstance();
TConnection connection = connectionFactory.newConnection( DATABASE_URI );
if ( !checkServerAndPrintSystemInformation( connection ) )
return;
TXMLObjectModel dom4jObjectModel = TDOM4JObjectModel.getInstance();
TXMLObjectModel.register( dom4jObjectModel );
TXMLObjectAccessor accessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( COLLECTION ) , dom4jObjectModel );
try {
TXMLObject xmlObject = TXMLObject.newInstance( TDOM4JObjectModel.getInstance() );
TXQuery xqueryall = TXQuery.newInstance(XQUERY);
performXQueryAndListPersons( accessor, xqueryall );
}
catch (TException taminoException) {
taminoException.printStackTrace();
}
connection.close();
}

private final static String DATABASE_URI = “http://localhost/tamino/mydb”;
private final static String XQUERY = “for $b in input()/person return $b”;
private final static String COLLECTION = “people”;
}

--------------------------

After I executed this code I got:

-------------
Connecting to Tamino database http://localhost/tamino/mydb, … server is alive

Here is some systeminformation
------------------------------

The Tamino server hosting http://localhost/tamino/mydb is version 4.1.1.1
(Server API version: 4.1, Tamino API for Java version: 4.1.1.1)

The query “for $b in input()/person return $b” returns no documents!
-------------

However in Tamino Interactive Interface, when execute the following xquery

-------------
for $b in input()/person
return $b
-------------

I got records from mydb database Tamino. So in TII, xquery works!

How should I fix my source code to make it works?
Any help will be appreciated.

In advance, thanks a lot!

best regards,
Dariusz Baumann

Hello!

I also have been trying to use “QUERY” and “JOIN” in java source code. Unfortunately, I couldn’t achieve any success. I attached my sample code for XQUERY.

-----------------------
SampleXQuery.java
bib-schema.TSD
bib.XML
reviews-schema.TSD
reviews.XML
-----------------------

It still doesn’t work. :frowning:

Please help me, in advance thanks a lot!

best regards,
Dariusz Baumann
sample_XQUERY.zip (3.12 KB)

Hello Dariusz,

I think that your code is correct, but there might be some issues with the schema/query.

Unfortunately, the zip file that you posted did not post correctly - when I try to download it I only get “broken.gif”!

However, I changed a couple of lines of code to access a schema that I already have in my system and I get the following results:

Connecting to Tamino database http://localhost/tamino/test, ... server is alive

Here is some systeminformation
------------------------------

The Tamino server hosting http://localhost/tamino/test is version 4.1.1.3
(Server API version: 4.1, Tamino API for Java version: 4.1.1.1)

The query "for $b in input()/A return $b" returns <xq:value/>
 documents, which are:
<A>
   <B b="1"/>
   <C>a</C>
   <D>alternative</D>
</A><A xmlns:ino="http://namespaces.softwareag.com/tamino/response2">
   <B b="3"/>
   <C>c</C>
   <D>drek</D>
</A>



Things to note are:
   - I didn’t change the code much - just the URL, the query and the print statement.
   - We have different Tamino versions (I have 4.1.1.3)
   - The result of [printing] the count is not quite what I would expect!

Perhaps you could try posting the zip file again, or just pasting the schema into a posting?

Thanks,
Trevor.

P.S. I don’t know if it is terribly important, but I used Java 1.3.1 (1.3.1_06-b01) and DOM4J 1.3 for the test.