How to iterate nodes one by one | Community
Skip to main content
Level 6
September 20, 2021
Solved

How to iterate nodes one by one

  • September 20, 2021
  • 3 replies
  • 6335 views

I am using the below code to iterate the Node and update the property but it ends up loading the infinite loop. 

resource -> launch.getResource().getChild("jcr:content")

 

Node node = resource.adaptTo(Node.class);
final NodeIterator it=node.getNodes();
do {
LOG.info("NODE PATH IS: "+node.getPath() );

node.setProperty(slingResourceType,conf/data/product/component);
node.getSession().save();
//it.nextNode();
}while(it.hasNext());

 

 

Is this the correct approach? It doesn't iterate from one node to other and print infinite time the log

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi, here is combination of your code and other guys that shares advise in this thread

Resource resource = launch.getResource().getChild("jcr:content");
Node node = resource.adaptTo(Node.class);
final NodeIterator it = node.getNodes();
while (it.hasNext()) {
    Node childNode = it.nextNode();
    childNode.setProperty(slingResourceType,conf/data/product/component);
}
session.save();

In your original code you were referring to root node inside do..while loop instead of using node returned by nextNode(), this is why you only get jcr:content node. Please also be aware that getNodes() returns iterator for direct child nodes only.

 

If it still does not work for you, please share update code, for further analysis

3 replies

Rohit-Negi
Level 3
September 20, 2021

Hi @ronnie09 ,

 

Use it.nextNode() in do loop to get the next node from nodeIterator object.

 

Thanks!

Anudeep_Garnepudi
Community Advisor
Community Advisor
September 20, 2021

@ronnie09 

Use while loop.

while(it.hasNext()) {

Node eachChildNode = it.nextNode();

eachChildNode.setProperty("...","...");

}

session.save();

AG
Ronnie09Author
Level 6
September 20, 2021

@anudeep_garnepudi 

 

I just keep revolving in jcr:content node only. It is not moving further nodes

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
September 20, 2021

Hi, here is combination of your code and other guys that shares advise in this thread

Resource resource = launch.getResource().getChild("jcr:content");
Node node = resource.adaptTo(Node.class);
final NodeIterator it = node.getNodes();
while (it.hasNext()) {
    Node childNode = it.nextNode();
    childNode.setProperty(slingResourceType,conf/data/product/component);
}
session.save();

In your original code you were referring to root node inside do..while loop instead of using node returned by nextNode(), this is why you only get jcr:content node. Please also be aware that getNodes() returns iterator for direct child nodes only.

 

If it still does not work for you, please share update code, for further analysis

arunpatidar
Community Advisor
Community Advisor
September 20, 2021