I found this post which fixed my problem:

http://www.nabble.com/getting-children%27s-content-td877636.html#a878397

The full code is:

import info.magnolia.cms.core.HierarchyManager;
import info.magnolia.context.WebContext;
import info.magnolia.cms.util.FactoryUtil;
import info.magnolia.context.MgnlContext;
import info.magnolia.cms.core.Content;
import info.magnolia.cms.core.ItemType;

import javax.jcr.RepositoryException;

String contentString = "";
            try{
                WebContext webContext = (WebContext)
FactoryUtil.newInstance(WebContext.class);
                webContext.init(request , response ,
request.getSession().getServletContext());
                MgnlContext.setInstance(webContext);

                // now you should be able to access HierarchyManager on this
context
                HierarchyManager websiteHierarchy =
webContext.getHierarchyManager("website");
                Content contentIndex =
websiteHierarchy.getContent("/index");
                System.out.println("title [" + contentIndex.getTitle()
+"]");

                Collection children =
contentIndex.getChildren(ItemType.CONTENTNODE);
                System.out.println("children size [" + children.size()
+"]");
                Iterator childrenIterator = children.iterator();
                while (childrenIterator.hasNext()) {
                    Content child = (Content) childrenIterator.next();
                    Iterator paraIterator =
child.getChildren(ItemType.CONTENTNODE).iterator();
                    while (paraIterator.hasNext()) {
                            Content para = (Content) paraIterator.next();
                            contentString +=
para.getNodeData("text").getString();
                    }
                }

                System.out.println("contentString [" + contentString +"]");

            } catch (RepositoryException re){
                re.printStackTrace();
            }




doahh wrote:
> 
> I came across this post which seems to be pretty much what I need:
> 
> http://www.nabble.com/For-a-Flash-element%3A-Need-manual-access-to-repository-td6185443.html#a6206233
> 
> but I get stuck while following it. I am attempting to create a servlet
> which when called will return the given page content. So I will pass in
> 'index' and get back the content of the page 'index'. So far I have this
> which seems to work:
> 
> WebContext webContext = (WebContext)
> FactoryUtil.newInstance(WebContext.class);
> webContext.init(request , response ,
> request.getSession().getServletContext());
> MgnlContext.setInstance(webContext);
> HierarchyManager websiteHierarchy =
> webContext.getHierarchyManager("website");
> Content contentIndex = websiteHierarchy.getContent("/index");
> System.out.println("title [" + contentIndex.getTitle() +"]");
> 
> and I get back the title correctly. What I would now like to do is
> retrieve the rest of the page content. I have been trying this:
> 
> Iterator cCollectionIterator = ((Content)
> contentIndex.getContent("prosocTextImage")).getChildren().iterator();
> while (cCollectionIterator.hasNext()) {
>         Content tmp = (Content) cCollectionIterator.next();
>         contentString += tmp.getNodeData("contentParagraph").getString();
>         System.out.println("contentString [" + contentString +"]");
> }
> 
> 'prosocTextImage' is the name of a paragraph but as I am really not sure
> what I should be passing in here I am getting an error:
> 
> javax.jcr.PathNotFoundException: prosocTextImage
> 
> Can anyone help me out please?
> 

-- 
View this message in context: 
http://www.nabble.com/Read-data-from-repo-tp24596835p24597203.html
Sent from the Magnolia - User mailing list archive at Nabble.com.


----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to