How to get content from components in jcr | Community
Skip to main content
Level 3
Oct 30, 2017 8:54 AM UTC
Solved

How to get content from components in jcr

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.
manoj_devapath
manoj_devapathAccepted solution
Level 5
Oct 30, 2017 4:11 PM UTC

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

Level 3
Nov 3, 2017 7:57 AM UTC

Can you explain how to?

Translate
manoj_devapath
Level 5
Nov 2, 2017 12:50 PM UTC

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

Translate
Level 3
Nov 2, 2017 7:35 AM UTC

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

Translate
Level 3
Nov 2, 2017 7:34 AM UTC

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.

Translate
VeenaVikraman
New Participant
Oct 30, 2017 6:39 PM UTC

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 ?

Translate
manoj_devapath
manoj_devapathAccepted solution
Level 5
Oct 30, 2017 4:11 PM UTC

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

      }

      }

    }

Translate
smacdonald2008
Level 10
Oct 30, 2017 3:52 PM UTC

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

Translate
Level 3
Oct 30, 2017 11:47 AM UTC

Can you give me some example code please?

Thanks so far.

Translate
viveksachdeva
New Participant
Oct 30, 2017 10:51 AM UTC

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

Translate