Basic Preferences Question - traversing nodes

B

Bruce

Can I supply a path to a given node and get the value in one call:
ie prefs.getInt("/node1/node2/entry", -1);

Or, do I need to traverse to the node first, then get the value:
Preferences node = prefs.node("/node1/node2");
node.getInt("entry", -1);

It seems I have to do it in two steps unless I am missing something.
That's what I would like to verify. (If that's the case, that's too
bad. I was hoping I could use xpath as well).
 
A

Adam P. Jenkins

Bruce said:
Can I supply a path to a given node and get the value in one call:
ie prefs.getInt("/node1/node2/entry", -1);

Or, do I need to traverse to the node first, then get the value:
Preferences node = prefs.node("/node1/node2");
node.getInt("entry", -1);

It seems I have to do it in two steps unless I am missing something.
That's what I would like to verify. (If that's the case, that's too
bad. I was hoping I could use xpath as well).

You're correct that to get the value of a key in a node requires two
function calls: first retrieve the Preferences node, then retrieve the key
value. If I'm just retrieving one value, I'll usually write it in one line,
as:

int val = prefs.node("node1/node2").getInt("entry", -1);

In practice though I rarely do this because I usually am fetching several
values from one node anyway, so I'm more likely to write:

Preferences node = prefs.node("node1/node2");
int val1 = node.getInt("entry1", -1);
int val2 = node.getInt("entry2", -1);
/* ... etc. */

So I don't see lack of get*() functions which take a path as any real
problem.

If you want to use XPath, check out the JXPath library at

http://jakarta.apache.org/commons/jxpath/

This library provides a framework to apply XPath expressions to any
hierarchical object model. The library comes with model implementations
which allow XPath queries on JavaBeans, Maps, Collections, and JDOM trees.
By implementing a few classes, you can make a new implementation for
java.util.prefs.Preferences.

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top