Hi Team, I have a requirement where my client wants one of my component to be appeared in customize way in page source(not the way it is rendering on page source as per the component rendering ) So I have to call a model class in footerlibs and iterate through the node of the component and making data available as he wished in page source. Node structure in jcr like below
as per above screen shot assume my component is name is "Acomponent". So I want to get the data on Acomponent where the first time my code got it.as per above ss it is "jcr:content/par/Acomponent"
Node parentNode = here Im getting the current page path and getting the node till pagepath+jcr:content
if(null!=ParentNode){
NodeIterator it = parentNode.getNodes("par*");
//instead of getting all the parentnodes including (par,custompar,par1) I want to get the node starting par (meant I want to get the par,par1 and par2 etc) and do the iteration further. But I tried NodeIterator it = parentNode.getNodes("par*"); and it is always getting Node named "par" only. here Im tryinng to skip of getting all the parent nodes like (custompar) for the efficiency of the code need to get the (par and par1). Any idea why it cant be done ?
}
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @JakeCham -
Have you tried below approach :
if (parentNode != null) {
NodeIterator it = parentNode.getNodes();
while (it.hasNext()) {
Node node = it.nextNode();
String nodeName = node.getName();
// Check if the node starts with "par" and is not "custompar"
if (nodeName.startsWith("par") && !nodeName.equals("custompar")) {\
}
}
}
OR
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
// Assuming you have access to the JCR Session
Session session = ...;
// Construct the SQL2 query to retrieve nodes starting with "par"
String queryStatement = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE('" + parentNode.getPath() + "') AND NAME() LIKE 'par%' AND NAME() <> 'custompar'";
try {
QueryManager queryManager = session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery(queryStatement, Query.JCR_SQL2);
NodeIterator result = query.execute().getNodes();
while (result.hasNext()) {
Node node = result.nextNode();
// Perform your desired logic with the node
// For example, access properties or iterate through child nodes
}
} catch (Exception e) {
// Handle any exceptions that may occur during the query execution
}
Try "getChildNodes()" with pattern.
NodeIterator it = parentNode.getChildNodes("par*");
If the above doesn't work, please take a look at JcrUtils opensource library
public static Iterable<javax.jcr.Node> getChildNodes(javax.jcr.Node node,
String pattern)
JcrUtils.getChildNodes(parentNode, "par*");
Hope this helps!
getChileNode method is not there in my 6.4sp.
To go on below way can u explain how to iterate this to get a Node please ?
JcrUtils.getChildNodes(parentNode, "par*");
Here is the example:
Node parentNode = resourceResolver.getResource("path").adaptTo(Node.class);
Iterator<Node> childNodes = JcrUtils.getChildNodes(parentNode, "par*").iterator();
while(childNodes.hasNext()) {
Node node = childNodes.next();
//Do your stuff
}
Thank you @Sudheer_Sundalam This helped..
Why don’t you run a query to filter the node based on sling:resourceType to be your component path for your page.once you get the reference of all of your component node you can iterate them any way as you want
Hello @JakeCham -
Have you tried below approach :
if (parentNode != null) {
NodeIterator it = parentNode.getNodes();
while (it.hasNext()) {
Node node = it.nextNode();
String nodeName = node.getName();
// Check if the node starts with "par" and is not "custompar"
if (nodeName.startsWith("par") && !nodeName.equals("custompar")) {\
}
}
}
OR
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
// Assuming you have access to the JCR Session
Session session = ...;
// Construct the SQL2 query to retrieve nodes starting with "par"
String queryStatement = "SELECT * FROM [nt:base] WHERE ISDESCENDANTNODE('" + parentNode.getPath() + "') AND NAME() LIKE 'par%' AND NAME() <> 'custompar'";
try {
QueryManager queryManager = session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery(queryStatement, Query.JCR_SQL2);
NodeIterator result = query.execute().getNodes();
while (result.hasNext()) {
Node node = result.nextNode();
// Perform your desired logic with the node
// For example, access properties or iterate through child nodes
}
} catch (Exception e) {
// Handle any exceptions that may occur during the query execution
}
Views
Likes
Replies