Anybody know how to return multiple elements/attributes using Tamino API for Java?
Thanks!
Jazz
Hello Jazz,
I’m afraid that I’m not sure I understand your question entirely.
The exact code required will depend upon the Object Model that you decide to use, but with the Tamino API for Java and the DOM object model you can do something like this:
TConnection connection = TConnectionFactory.getInstance().newConnection(databaseURI);
TXMLObjectAccessor accessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( collection ), TJDOMObjectModel.getInstance() );
TQuery query = TQuery.newInstance("/Telephone");
TResponse response = accessor.query(query);
TXMLObjectIterator iterator = response.getXMLObjectIterator();
TXMLObject xmlObject = null;
while ( iterator.hasNext() ) {
xmlObject = iterator.next();
xmlObject.writeTo(System.out);
}
connection.close();
The result of the XPath query “/Telephone” is all the Telephone documents, which could be considered a collection of elements and attributes.
If the query were “/Telephone/LoginName” then the results would be the LoginName elements from the Telephone documents. (Which can then be iterated in the same manner.)
Is this of any help?
Cheers,
Trevor.
Thanks a lot for the reply, Trevor. I am afraid I confused you with my question, sorry! OK, let me rephrase my question: Let’s assume that I have an element Person that has address and name as attributes. What I wanted to do is to find a person with a certain name and return both the address and the name using one XPath query. Is that possible at all?
Thanks again!
Jazz
Hi Jazz,
let’s make some example Person documents:
<Person name="Doh" address="12 Street"/>
<Person name="Ray" address="34 Avenue"/>
<Person name="Mee" address="56 Grove"/>
</pre><BR>If that is all that's in the documents, the easiest query would be:<BR><pre class="ip-ubbcode-code-pre">
/Person[@name="Mee"]
</pre><BR>The result of that query against Tamino will be:<BR><pre class="ip-ubbcode-code-pre"> <Person ino:id="X" name="Mee" address="56 Grove" />
</pre><BR>You are then at liberty to access the attributes in the most appropriate way.<BR><BR>However, I suspect that your documents are rather more complex and you want to enhance performance by only retrieving two attributes.....<BR><BR>One way of doing this would be with the following query:<BR><pre class="ip-ubbcode-code-pre"> /Person[@name="Mee"]/@name | /Person[@name="Mee"]/@address
</pre><BR>This query returns the union of the two XPath statements. Running against Tamino the results of this query will be *two* elements:<BR><pre class="ip-ubbcode-code-pre">
<Person ino:id="X" name="Mee" />
<Person ino:id="X" address="56 Grove" />
(Of course, selecting the name using ‘=’ and the exact name is also begging to be optimized! I suppose that your case will be using ‘~=’ and wildcards.)
Am I guiding you in the right direction now, or does this seem completely wrong?!
Cheers,
Trevor.
Thanks a lot, Trevors! Actually that’s what I am doing right now, while the problem seems to be that we are doing two queries at a time, or are we?
Jazz
Hi Jazz,
I think that the easier answer is “yes”!
Perhaps we should take a step back from this and I should ask: what exactly do you need to do in your code?
Maybe there is another way to do it…
Cheers,
Trevor.
Well, what I am trying to do exactly is to take an element and nest it in itself while with different attribute values for a few levels and then try to do an ancester-descendant query which should return an ancester with a certain attribute value and at the same time return all its descendants with a certain attribute value. I understand that I could do FLWR with QuiP, but its performance just sucks. The same query took me 10 minutes to execute via QuiP while it only took me about 4 seconds using API for Java. And besides, I need to measure the query execution time too.
Jazz