Expand my Community achievements bar.

SOLVED

How to iterate nodes one by one

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

6 Replies

Avatar

Level 4

Hi @Ronnie09 ,

 

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

 

Thanks!

Avatar

Community Advisor

@Ronnie09 

Use while loop.

while(it.hasNext()) {

Node eachChildNode = it.nextNode();

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

}

session.save();

Avatar

Level 7

@Anudeep_Garnepudi 

 

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Administrator

@lukasz-m Thank you for answering in the AEM community. Good to have AEM SMEs like you. 



Kautuk Sahni