> Not that I can see. However, you can write the following once, in a
> utility lib somewhere:
[quoted text clipped - 25 lines]
>
> Is that close to what you want?
> On second look, it isn't quite what I want. I don't want to populate
> a collection of Nodes, I want to populate a collection of Strings. I
> suppose the approach is to send the XPath to asList(), so get() can use
> it to evaluate() the node and return the resulting String?
Sure, that'll work.
Let me clarify something: I used the XPath instance to extract the text
from a node in my first reply only because it is more general. I
assumed that in practice you may want to perform more operations to
which XPath is well-suited. XPath is a way to find your way around an
XML file; it's not a replacement for DOM. If all you want is the text
of a node, just call getTextContent() on the Node, and you're done.
XPath is good for navigation, which is clumsy and painful in the DOM,
but XPath is clumsy and painful for direct operations on a single node
of an XML document.
In a less trivial example where XPath is really appropriate, it would
perhaps still be cleaner for the implementation of Nodes.asList to
construct its own XPath instance, since there's no reason to assume that
the original NodeList comes from XPath code and therefore no reason to
assume that the caller has an XPath instance to supply.
Also, you'll probably want to rename my Nodes.asList to something else,
since it no longer does what the name suggests.
If you again want to be somewhat general, you can write:
public static interface Filter<S,T>
{
public T exec(S arg);
}
public static <S,T> List<T> mapList(List<S> source, Filter<S,T> f)
{
List<T> r = new ArrayList<T>(source.size());
for (S from : source) r.add(f.exec(from));
return r;
}
and then,
col.addAll(MyUtil.mapList(
Nodes.asList(xpath.evaluate(...)),
new Filter<Node,String>() {
public String exec(Node arg)
{
return arg.getTextContent();
}
}));

Signature
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
Larry Coon - 13 Jun 2006 06:01 GMT
> Sure, that'll work.
>
> Let me clarify something:
(clarification snipped)
Thanks Chris, I appreciate all the help.
Larry Coon
University of California