How to limit the number of nodes returned using Node.getNodes() in AEM 5.6.1 . or is there any option to travese the child nodes with index ., say Node.getNode(index)., of Node.getFirstChild() ? This will be helpful in retreiving specific Node by its position, instead of getting all the nodes and then travesing ..
Solved! Go to Solution.
Views
Replies
Total Likes
Hi sakthiT
As far as i know, there is no way other than this. We need to Iterators over child nodes.
Example:-
java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer");
Iterator it = custNode.iterator();
//only going to be 1 content/customer node if it exists
if (it.hasNext())
{
//Count the number of child nodes to customer
Node customerRoot = content.getNode("customer");
Iterable itCust = JcrUtils.getChildNodes(customerRoot);
Iterator childNodeIt = itCust.iterator();
//Count the number of customer child nodes
while (childNodeIt.hasNext())
{
childRecs++;
childNodeIt.next();
}
I hope this will answer your question.
Thanks and Regards
Kautuk Sahni
Views
Replies
Total Likes
Hi,
If you need only limited results you can use Query Builder API. Apart from this why would you need node at specified index without knowing what it contains?
This query accepts limit value:
Views
Replies
Total Likes
Hi sakthiT
As far as i know, there is no way other than this. We need to Iterators over child nodes.
Example:-
java.lang.Iterable<Node> custNode = JcrUtils.getChildNodes(content, "customer");
Iterator it = custNode.iterator();
//only going to be 1 content/customer node if it exists
if (it.hasNext())
{
//Count the number of child nodes to customer
Node customerRoot = content.getNode("customer");
Iterable itCust = JcrUtils.getChildNodes(customerRoot);
Iterator childNodeIt = itCust.iterator();
//Count the number of customer child nodes
while (childNodeIt.hasNext())
{
childRecs++;
childNodeIt.next();
}
I hope this will answer your question.
Thanks and Regards
Kautuk Sahni
Views
Replies
Total Likes
Hi,
JCR API does not provide any direct method.
You can use getNodes(java.lang.String namePattern), if you are following some specific pattern in node names.
I guess you can build your own getIndex() method as well with the help of iterator, but its complexity will be O(n). It also depends upon how many nodes you have and how many times you make this request.
I still feel, you can get a better answer from community if you share your use case.
Views
Replies
Total Likes
Hi,
what's the reason, that you need to access (let's say) the 3rd childnode of a node? I cannot think of any reason doing this. When you design your structure use names instead of relying on the order of specific subnodes.
kind regards,
Jörg
Views
Replies
Total Likes
Write a method that queries nodes (using JCR SQL2 for example) and then reduce the Node result set. This will make the result set a lot smaller and quicker to iterate.
Views
Replies
Total Likes
I see SQL Query could help in this situation, but how much is it better on performance aspect ?
Views
Replies
Total Likes
Hi,
These 2 great post will help you to answer your question on query builder performance:-
I guess, you can perform some test as well on your own to compare iteration in JCR node vs query builder.
Thanks
Views
Replies
Total Likes
Hi,
iterating the child nodes is definitly faster than finding them via JCR query.
kind regards,
Jörg
Views
Replies
Total Likes
Views
Likes
Replies