Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to get content from components in jcr

Avatar

Level 4

Hello guys,

I have the following node structure it can vary depending on how the author created the page. I want to query the content of each headline, textfield, image or link field.

It should just be the last node in every branch the type and depth is not important and fixed.

How can I achieve this starting from the root node "press-releases"?

This only delivers the next node for example "par", but I don't want to iterate through all until I have the last child node of a branch.

NodeIterator subnodes = node.getNodes();

1336748_pastedImage_1.png

1 Accepted Solution

Avatar

Correct answer by
Level 6

I understand you can use query to get node or results as aspected but it costs more burden on server. so if content is less pls go head and use query if not pls use node iteration logic

sample sudo code

NodeIterator subnodes = result.getNodes();

    StringBuilder output = new StringBuilder();

    while (nodes.hasNext()) {

      Node node = nodes.nextNode();

      if(node!= null){

            String nodeName= node.getName();

      if(nodeName.contains("headline")||nodeName.contains("textfield")||nodeName.contains("image") ||nodeName.contains("link")) {

      //busniess logic

      }

      }

    }

View solution in original post

9 Replies

Avatar

Community Advisor

I believe you need to write a query which should have following things:

- path : root from where you want your search

- ORing of all expected resource types(I believe this one is better than using node name like highlight/image,etc)

This will give you list of all such nodes and you can perform whatever operation there on

Avatar

Level 4

Can you give me some example code please?

Thanks so far.

Avatar

Level 10

There are lots of examples on the internet of using JCR SQL2 to iterate through nodes and retrieve values. For example:

http://drfits.com/jcr-sql2-query-with-examples/

JCR Query Cheat Sheet - Community Wiki - Magnolia

Avatar

Correct answer by
Level 6

I understand you can use query to get node or results as aspected but it costs more burden on server. so if content is less pls go head and use query if not pls use node iteration logic

sample sudo code

NodeIterator subnodes = result.getNodes();

    StringBuilder output = new StringBuilder();

    while (nodes.hasNext()) {

      Node node = nodes.nextNode();

      if(node!= null){

            String nodeName= node.getName();

      if(nodeName.contains("headline")||nodeName.contains("textfield")||nodeName.contains("image") ||nodeName.contains("link")) {

      //busniess logic

      }

      }

    }

Avatar

Community Advisor

Hi Tim

  Is there any specific use-case that you want to query this page level (your headline component, text component, link component should render their properties component level right ?)?  Could you please help me understand the exact requirement ?

Avatar

Level 4

Hi Veena,

I want to export the content in some structure and feed it to an android app maybe. So I want to extract all headlines, images und text field and put them in a new html structure and the app just call it and display it.

Avatar

Level 4

But does it iterate to every sub node? I think it iterates only the next child nodes of the given node.

Avatar

Level 6

yes for now this sudo code iterates only child nodes. To iterate every sub node we have to update logic.