-
Notifications
You must be signed in to change notification settings - Fork 30
Notes
Sebastian Menski edited this page May 6, 2014
·
5 revisions
With a map
function:
Set<String> names = S("...")
.xpath("/customer")
.map(new Mapper<String>() {
public String map(Spin s) {
return s.attribute("name");
}
});
Without a map
function:
Set<String> names = new HashSet<String>();
for(Spin customer : S("...").xpath("/customer")) {
names.add(customer.attribute("name"));
}
A simple filter / map pipeline:
List<String> customerNames = S(xmlString)
.xpath("/customer")
.filter(new Filter() {
public boolean filter(Spin s) {
return s.attribute("rating").asInt() > 100;
}
}).map(new Mapper<String>() {
public String map(Spin s) {
return s.attribute("name");
}
});
We could also provide a set of default mappers for attributes etc.:
List<Integer> ratings = S(xmlString).map(attribute("rating", asInt()));
highRatedCustomers = filter(lambda s: s.attribute('rating').asInt() > 100, S('...').xpath("/customer"))
highRatedCustomersNames = map(lambda s: s.attribute('name'), highRatedCustomers)
numberOfHighRatedCustomersWhereNameStartsWithA = len([name for name in highRatedCustomersNames if name.lower().startswith('a')])
or
def highRating(s):
return s.attribute('rating') > 100
oneline = len([
customer for customer in filter(highRating, S('...').xpath('/customer')) if customer.attribute('name').lower().startswith('a')
])
Url-like syntax with query parameters
S( myJavaClass ).transformTo("data-format://json?dateFormat='YYYY-MM-dd'&indent");
Or a more "typed" approach
DataFormat json = JSON.config("dateFormat", "YYYY-MM-dd").config("indent");
DataFormat json = JSON.dateFormat("YYYY-MM-dd").indent();
S( myJavaClass ).transformTo(json);