How to get content from components in jcr | Community
Skip to main content
Level 3
October 30, 2017
Solved

How to get content from components in jcr

  • October 30, 2017
  • 9 replies
  • 4048 views

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();

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by manoj_devapath

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

      }

      }

    }

9 replies

viveksachdeva
Community Advisor
Community Advisor
October 30, 2017

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

Level 3
October 30, 2017

Can you give me some example code please?

Thanks so far.

smacdonald2008
Level 10
October 30, 2017

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

manoj_devapath
manoj_devapathAccepted solution
Level 5
October 30, 2017

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

      }

      }

    }

VeenaVikraman
Community Advisor
Community Advisor
October 30, 2017

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 ?

Level 3
November 2, 2017

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.

Level 3
November 2, 2017

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

manoj_devapath
Level 5
November 2, 2017

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

Level 3
November 3, 2017

Can you explain how to?