The following section gives some examples of how node access and property read operations are performed. In order to provide some context for the examples, we return to our earlier example of a repository structured like this:
Node
Property = "Some Value"
[root]
└─products
├─jcr:created = "2001-01-01T..."
├─jcr:content
│ ├─myapp:title = "Our Products..."
│ ├─myapp:lead = "Geometrixx is proud..."
│ ├─myapp:paragraph[1]
│ │ ├─myapp:text = "Geometrixx is..."
│ │ └─ myapp:image = [binary data]
│ └─myapp:paragraph[2]
│ ├─ myapp:text = "We have..."
│ └─ myapp:image = [binary data]
└─rhombus
├─jcr:created = "2002-06-01T"
└─jcr:content
├─myapp:title = "Rhombus:..."
├─myapp:price = "123.00"
├─myapp:lead = "Here at..."
├─myapp:paragraph[1]
│ ├─myapp:text = "The rhombus..."
│ └─myapp:image = [binary data]
└─myapp:paragraph[2]
├─myapp:text = "Some say..."
└─myapp:image = [binary data]
Assuming that the programmer has called:
Session session = ...
Node root = session.getRootNode();
From the root node, one can access any node or property in the workspace. For example,
Node n1 = root.getNode("products");
Node n2 = n1.getNode("rhombus");
Node n3 = n2.getNode("jcr:content");
Node n4 = n3.getNode("myapp:paragraph[2]");
Property p = n4.getProperty("myapp:text");
Value v = p.getValue();
String s = v.getString();
System.out.println(s);
would print, "Some say..." to standard output. Alternatively, more convenient direct access is also possible,
Property p = root.getProperty("products/rhombus/
jcr:content/myapp:paragraph[2]/myapp:text");
System.out.println(p.getString());
Here we use a relative path from the root to access a property deep in the hierarchy.
As well, traversal of the hierarchy is easily done. For example, given the following method,
public void traverse(Node n, int level)
throws RepositoryException {
String name = (n.getDepth() == 0) ? "/" : n.getName();
System.out.println(makeIndent(level) + name);
for (PropertyIterator i = n.getProperties();
i.hasNext();) {
Property p = i.nextProperty();
System.out.println(makeIndent(level + 1) +
p.getName() + " = \"" +
p.getString() + "\"");
}
for (NodeIterator i = n.getNodes(); i.hasNext();) {
Node nn = i.nextNode();
traverse(nn, level + 1);
}
}
the call,
traverse(root, 0);
would print out something like the following:
/
products
jcr:created = "2001-01-01T..."
jcr:content
myapp:title = "Our Products..."
myapp:lead = "Geometrixx is proud..."
myapp:paragraph[1]
myapp:text = "Geometrixx is..."
myapp:image = ""
myapp:paragraph[2]
myapp:text = "We have..."
myapp:image = ""
rhombus
jcr:created = "2002-06-01T..."
jcr:content
myapp:title = "Rhombus:..."
myapp:price = "123.00"
myapp:lead = "Here at..."
myapp:paragraph[1]
myapp:text = "The rhombus..."
myapp:image = ""
myapp:paragraph[2]
myapp:text = "Some say..."
myapp:image = ""