How to iterate child nodes of a node in AEM

Posted By: Matpal - June 12, 2017

Following example JSP tells you how to iterate a node and access its child nodes. For example we will display "jcr:primaryType" property of nodes here.


<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>
<%@ page import="java.util.Iterator,
     com.day.cq.wcm.api.PageFilter,
  com.day.cq.wcm.api.Page,
  com.day.cq.wcm.api.PageManager,
  com.day.cq.wcm.api.WCMMode,
  org.apache.sling.api.resource.Resource,
  javax.jcr.Node,com.day.cq.replication.Replicator" %>

<%

String nodePath = "/content/community-components/en";

try
{
Node node = null;
NodeIterator nodes =null;
String result="";
resource = slingRequest.getResourceResolver().getResource(nodePath);

    if(resource != null)
    {
    node = resource.adaptTo(Node.class);
    NodeIterator nodeItr = node.getNodes();
    
        while(nodeItr.hasNext())
                 {

                 Node cNode = nodeItr.nextNode();
                 result=cNode.getProperty("jcr:primaryType").getValue().getString();
                 out.print(result);

                 }

    }
}

catch(Exception e) 
{
out.print(e);
}
%>

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.