JXPath has a number of interesting uses. One very interesting use is to query java collections. So, instead of iterating over a collection and doing lots of if foo then bar, you can instead do it in a succinct XPath query.
Here's a quick code sample I grabbed from an onjava.com article http://www.onjava.com/pub/a/onjava/2004/12/22/jakarta-gems-1.html?page=2:
// assume that the "people" collection contains a list of Person objects.
// This is a list of Person who have a value of "US" for person.getCountry(), and a value greater than 1000000 for person.getJob().getSalary().
List richUsPeople = queryCollection( "~np~.[@country = 'US'?]/job@salary?/..~/np~", people );
// This is a list of Person who have a value of "GB" for person.getCountry(), and a value of "Tony" for person.getName().
List britishTony = queryCollection( "~np~.[@country = 'GB' and @name = 'Tony'?]~/np~", people );
// this is a list of String which contains all the results of person.getJob().getName() for all person objects in the people collection.
List jobNames = queryCollection( query3, "./job/name" );
In terms of efficiency, it's obviously not going to be any more efficient than simply iterating. But, if you're doing this on a small scale then it can make the code cleaner.
The code for queryCollection is:
import org.apache.commons.jxpath.JXPathContext;
public List queryCollection(String xpath,
Collection col) {
List results = new ArrayList();
JXPathContext context = JXPathContext.newContext( col );
Iterator matching = context.iterate( xpath );
while( matching.hasNext() ) {
results.add( matching.getNext() );
}
return results;
}
Here's a quick code sample I grabbed from an onjava.com article http://www.onjava.com/pub/a/onjava/2004/12/22/jakarta-gems-1.html?page=2:
// assume that the "people" collection contains a list of Person objects.
// This is a list of Person who have a value of "US" for person.getCountry(), and a value greater than 1000000 for person.getJob().getSalary().
List richUsPeople = queryCollection( "~np~.[@country = 'US'?]/job@salary?/..~/np~", people );
// This is a list of Person who have a value of "GB" for person.getCountry(), and a value of "Tony" for person.getName().
List britishTony = queryCollection( "~np~.[@country = 'GB' and @name = 'Tony'?]~/np~", people );
// this is a list of String which contains all the results of person.getJob().getName() for all person objects in the people collection.
List jobNames = queryCollection( query3, "./job/name" );
In terms of efficiency, it's obviously not going to be any more efficient than simply iterating. But, if you're doing this on a small scale then it can make the code cleaner.
The code for queryCollection is:
import org.apache.commons.jxpath.JXPathContext;
public List queryCollection(String xpath,
Collection col) {
List results = new ArrayList();
JXPathContext context = JXPathContext.newContext( col );
Iterator matching = context.iterate( xpath );
while( matching.hasNext() ) {
results.add( matching.getNext() );
}
return results;
}
Version 3.1 last modified by Geoff Fortytwo on 14/05/2008 at 00:42
Document data
Attachments:
No attachments for this document