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

get all the child node name in the parent nodepath

Avatar

Avatar
Validate 10
Level 2
sunitac70336244
Level 2

Likes

7 likes

Total Posts

65 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 1
View profile

Avatar
Validate 10
Level 2
sunitac70336244
Level 2

Likes

7 likes

Total Posts

65 posts

Correct Reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 1
View profile
sunitac70336244
Level 2

12-07-2019

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.

View Entire Topic

Avatar

Avatar
Validate 1
Level 5
Tuhin_Ghosh
Level 5

Likes

36 likes

Total Posts

301 posts

Correct Reply

40 solutions
Top badges earned
Validate 1
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Validate 1
Level 5
Tuhin_Ghosh
Level 5

Likes

36 likes

Total Posts

301 posts

Correct Reply

40 solutions
Top badges earned
Validate 1
Give Back 50
Give Back 5
Give Back 3
Give Back 25
View profile
Tuhin_Ghosh
Level 5

14-07-2019

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