Skip to main content
Level 6
Sep 20, 2021 11:43 AM UTC
Solved

How to iterate nodes one by one

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.
lukasz-m
lukasz-mAccepted solution
Community Advisor
Sep 20, 2021 2:03 PM UTC

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

6 replies

Rohit-Negi
Level 3
Sep 20, 2021 12:24 PM UTC

Hi @ronnie09 ,

 

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

 

Thanks!

Translate
Anudeep_Garnepudi
Community Advisor
Community Advisor
Sep 20, 2021 12:38 PM UTC

@ronnie09 

Use while loop.

while(it.hasNext()) {

Node eachChildNode = it.nextNode();

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

}

session.save();

Translate
AG
Ronnie09Author
Level 6
Sep 20, 2021 12:44 PM UTC

@anudeep_garnepudi 

 

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

Translate
lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
Sep 20, 2021 2:03 PM UTC

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

Translate
arunpatidar
Community Advisor
Community Advisor
Sep 20, 2021 3:41 PM UTC