Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

get all the child node name in the parent nodepath

sunitac70336244
Level 4
Level 4

Dear All,

I have below content structure in AEM-6.4

1789974_pastedImage_1.png

My parent content nodepath is /content/VignetteContent/plan

Now my requirement is that I want all the child nodes like plan-104105 , location-9999 etc. with the JCR property Value.I have written below code. But I am not getting.

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// localhost:4502/bin/sunita/myServlet.html?planID=104105&locationID=9999

@SlingServlet(paths = "/bin/sunita/myServlet", extensions = "html", methods = "GET")

public class TitleSlingServlet extends SlingAllMethodsServlet {

Logger log = LoggerFactory.getLogger(this.getClass());
private static final long serialVersionUID = 1L;
ResourceResolver resourceResolver;

public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/html");
  try {

   String planID = request.getParameter("planID");
   String locationID = request.getParameter("locationID");
   log.info("planID is *********===*****  " + planID + " +++locationID is *********===*****  " + locationID);

   // Get the resource (a node in the JCR) using ResourceResolver from the request
   resourceResolver = request.getResourceResolver();
   Resource resource = resourceResolver.getResource("/content/VignetteContent/plan");
   Iterator<Resource> children = resource.listChildren();
   log.info(" children for nodepath is *****=== " + children.hashCode());
   while (children.hasNext()) {
    Resource child = children.next();
    String childNodeName = child.getName();
    log.info(" childNodeName is *****=== " + childNodeName);

   }

  
  }

  catch (Exception e) {
   response.getWriter().println("<br />Can't read planID/locationID. make sure the suffix path exists!");
   log.error(e.getMessage());
  }
  response.getWriter().close();

}

}

Please help me on this.

1 Accepted Solution
Tuhin_Ghosh
Correct answer by
Level 7
Level 7

sunitac70336244​,

It seems there might be multiple nodes under the parent node that you have. If you know that there would always be two levels of nodes inside "/content/VignetteContent/plan" node you could continue using this code with a little twich.

Other wise if you are not sure on the number of levels, you should be using a query to get all the nodes.

resourceResolver = request.getResourceResolver();

  Session session1 = resourceResolver.adaptTo(Session.class);

  if (session1.nodeExists("/content/VignetteContent/plan")) {

  javax.jcr.Node parentNode = session1.getNode("/content/VignetteContent/plan");

  if (parentNode.hasNodes()) {

  java.util.Iterator<javax.jcr.Node> levelOnechildren = parentNode.getNodes();

  while (levelOnechildren.hasNext()) {

  javax.jcr.Node planNode = levelOnechildren.next();

  log.info(" planNode name is *****=== " + planNode.getName());

  if (planNode.hasNodes()) {

  java.util.Iterator<javax.jcr.Node> leveltwochildren = planNode.getNodes();

  while (leveltwochildren.hasNext()) {

  javax.jcr.Node locationNode = leveltwochildren.next();

  log.info(" Location Node name is *****=== " + locationNode.getName());

  }

  }

  }

  }

  }

Please be advised that there might be compilation errors as I have written this snipped in a simple note pad.

Hope this helps.

Thanks

Tuhin

View solution in original post

2 Replies
Tuhin_Ghosh
Correct answer by
Level 7
Level 7

sunitac70336244​,

It seems there might be multiple nodes under the parent node that you have. If you know that there would always be two levels of nodes inside "/content/VignetteContent/plan" node you could continue using this code with a little twich.

Other wise if you are not sure on the number of levels, you should be using a query to get all the nodes.

resourceResolver = request.getResourceResolver();

  Session session1 = resourceResolver.adaptTo(Session.class);

  if (session1.nodeExists("/content/VignetteContent/plan")) {

  javax.jcr.Node parentNode = session1.getNode("/content/VignetteContent/plan");

  if (parentNode.hasNodes()) {

  java.util.Iterator<javax.jcr.Node> levelOnechildren = parentNode.getNodes();

  while (levelOnechildren.hasNext()) {

  javax.jcr.Node planNode = levelOnechildren.next();

  log.info(" planNode name is *****=== " + planNode.getName());

  if (planNode.hasNodes()) {

  java.util.Iterator<javax.jcr.Node> leveltwochildren = planNode.getNodes();

  while (leveltwochildren.hasNext()) {

  javax.jcr.Node locationNode = leveltwochildren.next();

  log.info(" Location Node name is *****=== " + locationNode.getName());

  }

  }

  }

  }

  }

Please be advised that there might be compilation errors as I have written this snipped in a simple note pad.

Hope this helps.

Thanks

Tuhin

View solution in original post